# 446. Arithmetic Slices II - Subsequence

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LekNH5IywF8mjBxFcnu%2Fuploads%2FRpxpEw0BWv2iH0cGlRq5%2Fimage.png?alt=media\&token=22dbcbbb-d705-478f-b642-d322e1b7996b)

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LekNH5IywF8mjBxFcnu%2Fuploads%2FzC1sFp6EGpztJt7qORb7%2Fimage.png?alt=media\&token=0a3a8ef9-33a9-4d2d-9bce-5337eca574c2)

```java
class Solution {
    public int numberOfArithmeticSlices(int[] nums) {
        int n = nums.length;
        Map<Long, Integer>[] dp = new HashMap[n];
        
        int res = 0;
        for (int i = 0; i < n; i++) {
            dp[i] = new HashMap<>();
            for (int j = 0 ; j < i ; j++) {
                long diff  = (long)nums[i] - (long)nums[j];
                
                // dp[i][diff] = dp[j][diff]+1
                int value = dp[i].getOrDefault(diff, 0) + dp[j].getOrDefault(diff, 0) + 1;
                dp[i].put(diff, value);
                res += dp[j].getOrDefault(diff, 0);
            }
        }
        return res;
        
    }
}

/*
[2,4,    6,   8,   10]
   2:1
        2:2
   res+1[2,4,6]
      res+2   2:3 [2,4,6,8.   4,6,8]
             
*/
```
