> For the complete documentation index, see [llms.txt](https://timmybeeflin.gitbook.io/cracking-leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://timmybeeflin.gitbook.io/cracking-leetcode/dynamic-programming/446.-arithmetic-slices-ii-subsequence.md).

# 446. Arithmetic Slices II - Subsequence

![](/files/JknOQdHRv243e7PTg8hw)

![](/files/qg0lLGuweztix8bgVx5S)

```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]
             
*/
```
