Arithmetic Slices

Medium

Topics
ArrayDynamic Programming

An arithmetic slice is a contiguous subarray of length at least 3 with a constant difference between consecutive elements. Return the number of arithmetic slices in nums.

Example 1

Input:  nums = [1,2,3,4]
Output: 3

Example 2

Input:  nums = [1,3,5,7,9]
Output: 6

Example 3

Input:  nums = [1,2,3,8,9,10]
Output: 2

Constraints

  • 1 <= nums.length <= 5000
  • -1000 <= nums[i] <= 1000
Run ⌘' · Submit ⌘⏎