# Leetcode 413 - Arithmetic Slices

Note:

• Subarray/subsequence problems are usually dp.
• dp[i] means the longest arithmetic progression ending with nums[i].
• When nums[i-2], nums[i-1], nums[i] forms an arithmetic array, dp[i] = dp[i-1] + 1.
• If not, dp[i] = 2, and we add dp[i-1] to the ans[] to calculate the last longest length of arithmetic array.
• Finally, use math (n - 3 + 1)*(n - 3 + 2) / 2 to calculate the num of subarrays when the arithmetic array’s length is n.

Question:

An integer array is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.

For example, [1,3,5,7,9], [7,7,7,7], and [3,-1,-5,-9] are arithmetic sequences.
Given an integer array nums, return the number of arithmetic subarrays of nums.

A subarray is a contiguous subsequence of the array.

Example:

Code: