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.

&nbsp;
Example 1:

Input: nums = [1,2,3,4]
Output: 3
Explanation: We have 3 arithmetic slices in nums: [1, 2, 3], [2, 3, 4] and [1,2,3,4] itself.


Example 2:

Input: nums = [1]
Output: 0


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 5000
	-1000 &lt;= nums[i] &lt;= 1000


#Baraa
class Solution:
    def numberOfArithmeticSlices(self, nums: List[int]) -> int:
        """
        what is (x * (x + 1)) // 2? this is series sum formula between n and 1

        which means: n + (n - 1) + (n - 2) + (n - 3) .... + 1

        when we have a total number of 10 elements that form an arithmetic sum
        eg: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

        we can form subarrays up until count - 2
        which means 1,2,3,4,5,6,7,8
        if we take 7 for example --> [7, 8, 9, 10] or [7, 8, 9]

        we cant take [9, 10] as subarray as its length < 3
        """

        #this is done for termination only
        nums += [-float("inf")]
        n = len(nums)
        #base case
        if n < 3:
            return 0
        res = 0
        #store inital difference we have
        prev_diff = nums[1] - nums[0]
        #because we calculated an initial difference we store 2 inside count
        #which represents number of elements
        count = 2
        for i in range(2, n):
            diff = nums[i] - nums[i - 1]
            if prev_diff != diff:
                x = count - 2
                res = res + (x * (x + 1)) // 2
                prev_diff = diff
                count = 2
            else:
                count += 1
                prev_diff = diff
        return res
class Solution {
    public int numberOfArithmeticSlices(int[] nums) {
        int n = nums.length;
        if (n < 3) {
            return 0;
        }
        int[] dp = new int[n - 1];
        dp[0] = nums[1] - nums[0];
        for (int i = 2; i < n; i++) {
            dp[i - 1] = nums[i] - nums[i - 1];
        }
        int si = 0;
        int count = 0;
        for (int i = 1; i < n - 1; i++) {
            if (dp[i] == dp[i - 1]) {
                count += (i - si);
            } else {
                si = i;
            }
        }
        return count;
    }
}
class Solution {
public:
    int numberOfArithmeticSlices(vector<int>& nums) {
        int n=nums.size(),i,j,count=0,comm_diff;
        for(i=0;i<=n-3;i++)
        {
            comm_diff = nums[i+1]-nums[i];
            for(j=i+1;j<n;j++)
            {
                if(nums[j]-nums[j-1]==comm_diff)
                {
                    if((j-i+1)>=3)
                    {
                        count++;
                    }
                }
                else
                {
                    break;
                }
            }
        }
        return count;
    }
};
/**
 * @param {number[]} nums
 * @return {number}
 */
var numberOfArithmeticSlices = function(nums) {
    nums.push(-2000)
    let count = 0  
    let dist = nums[1]-nums[0]
    let start = 0
    
    for (let i = 1; i < nums.length; i++) {
        if (nums[i]-nums[i-1] !== dist) {
            
            let len =  i - start
            start = i-1
            dist = nums[i] - nums[i-1]
            
            // count the length of each largest AS and add to sum 
            // of len = 5 => the number of sub AS = 1 (len = 5) + 2 (len = 4) + 3 (len = 3)
            for (let k = 3; k <=len; k++) {
                count += len - k + 1
            }
            
        }
    }
    
    return count
};
Arithmetic Slices