Given an integer array nums, return the number of all the arithmetic subsequences of nums.

A sequence of numbers 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.
	For example, [1, 1, 2, 5, 7] is not an arithmetic sequence.


A subsequence of an array is a sequence that can be formed by removing some elements (possibly none) of the array.


	For example, [2,5,10] is a subsequence of [1,2,1,2,4,1,5,10].


The test cases are generated so that the answer fits in 32-bit integer.

&nbsp;
Example 1:

Input: nums = [2,4,6,8,10]
Output: 7
Explanation: All arithmetic subsequence slices are:
[2,4,6]
[4,6,8]
[6,8,10]
[2,4,6,8]
[4,6,8,10]
[2,4,6,8,10]
[2,6,10]


Example 2:

Input: nums = [7,7,7,7,7]
Output: 16
Explanation: Any subsequence of this array is arithmetic.


&nbsp;
Constraints:


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


class Solution:
    def numberOfArithmeticSlices(self, nums: List[int]) -> int:
        sz, dp, ans = len(nums), [defaultdict(int) for _ in range(len(nums))], 0
        for i in range(1, sz):
            for j in range(i):
                difference = nums[i] - nums[j]
                dp[i][difference] += 1
                if difference in dp[j]:
                    dp[i][difference] += dp[j][difference]
                    ans += dp[j][difference]
        return ans
class Solution {
    public int numberOfArithmeticSlices(int[] nums) {
        int n=nums.length;
        
        HashMap<Integer,Integer> []dp=new HashMap[n];
        
        for(int i=0;i<n;i++){
            dp[i]=new HashMap<Integer,Integer>();
        }
        
        int ans=0;
        
        for(int i=1;i<n;i++){
            for(int j=0;j<i;j++){
                long cd=(long)nums[i]-(long)nums[j];
                
                if(cd<=Integer.MIN_VALUE || cd>=Integer.MAX_VALUE){
                    continue;
                }
                
                int endingAtj=dp[j].getOrDefault((int)cd,0);
                int endingAti=dp[i].getOrDefault((int)cd,0);
                
                ans+=endingAtj;
                
                dp[i].put((int)cd,endingAtj+endingAti+1);
            }
        }
        
        return ans;
    }
}
class Solution {
public:
    int numberOfArithmeticSlices(vector<int>& nums) {
        int n=nums.size(),ans=0;
        unordered_map<long long, unordered_map<int,int>> ma;   // [diff, [index, count]]
        unordered_map<int,int> k;
        for(int i=1;i<n;i++)
        {
            for(int j=0;j<i;j++)
            {
                long long diff= (long long)nums[i]-(long long)nums[j];
                if(ma.find(diff)==ma.end())
                    ma[diff]= k;
                if(ma[diff].find(j)==ma[diff].end())
                    ma[diff][j]=0;
                ma[diff][i] += ma[diff][j] + 1;
                ans += ma[diff][j];
            }
        }
        return ans;
    }
};
/**
 * @param {number[]} nums
 * @return {number}
 */
var numberOfArithmeticSlices = function(nums) {
  let dp = new Array(nums.length);
  for(let i = 0; i < nums.length; i++) {
    dp[i] = new Map();
  }
  let ans = 0;
  for(let j = 1; j < nums.length; j++) {
    for(let i = 0; i < j; i++) {
      let commonDifference = nums[j] - nums[i];
      if ((commonDifference > (Math.pow(2, 31) - 1)) || commonDifference < (-Math.pow(2, 31))) {
        continue;
      }
      let apsEndingAtI = dp[i].get(commonDifference) || 0
      let apsEndingAtJ = dp[j].get(commonDifference) || 0

      dp[j].set(commonDifference, (apsEndingAtI + apsEndingAtJ + 1));
      ans += apsEndingAtI;
    }
  }
  return ans;
};
Arithmetic Slices II - Subsequence