<p>Given an integer array <code>nums</code>, return <em>the number of all the <strong>arithmetic subsequences</strong> of</em> <code>nums</code>.</p>

<p>A sequence of numbers is called arithmetic if it consists of <strong>at least three elements</strong> and if the difference between any two consecutive elements is the same.</p>

<ul>
	<li>For example, <code>[1, 3, 5, 7, 9]</code>, <code>[7, 7, 7, 7]</code>, and <code>[3, -1, -5, -9]</code> are arithmetic sequences.</li>
	<li>For example, <code>[1, 1, 2, 5, 7]</code> is not an arithmetic sequence.</li>
</ul>

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

<ul>
	<li>For example, <code>[2,5,10]</code> is a subsequence of <code>[1,2,1,<strong><u>2</u></strong>,4,1,<u><strong>5</strong></u>,<u><strong>10</strong></u>]</code>.</li>
</ul>

<p>The test cases are generated so that the answer fits in <strong>32-bit</strong> integer.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,4,6,8,10]
<strong>Output:</strong> 7
<strong>Explanation:</strong> 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]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [7,7,7,7,7]
<strong>Output:</strong> 16
<strong>Explanation:</strong> Any subsequence of this array is arithmetic.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1&nbsp; &lt;= nums.length &lt;= 1000</code></li>
	<li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>
</ul>

class Solution:
    def numberOfArithmeticSlices(self, nums: List[int]) -> int:

        # dp solution n^2

        # at each index, we will have to store how many subsequences start at that index,
        # what the differences of those subsequences are, and the lengths of those subsequences
        # for the length, we will keep track if its length 1, 2, or anything 3 and above,
        # anything 3 and above is considered arithmetic

       
        dp = {}
        # at the last index in nums, for the subseqeunces with difference of 0, 
        # there are [one of length 1 (just itself), 0 of length 2, 0 of length 3 or more]
        dp[len(nums) - 1] = {0 : [1, 0, 0]}

        # keep track of all arithmetic sequences
        total = 0

        # lets iterate from second last index (last already in dp) to first index
        for i in range(len(nums) - 2, -1, -1):

            # at every value there will always be a subsequence of 1, with difference 0
            # (just value by itself)
            dp[i] = {0: [1, 0, 0]}

            # keep track at each index how many ARITHMETIC sequences there are
            arithmetic_seq = 0
            # keep track of the differences we already added to our arithmetic total
            diffs_added = {}
            
            # compare value at i with all the subsequences after i
            for x in range(i + 1, len(nums)):
                # get the difference of value at i and value at x
                diff = nums[x] - nums[i]

                # add difference to dp[i] if its not already in 
                if diff not in dp[i]:
                    dp[i][diff] = [0, 0, 0]

                # check if index at x, has any subsequences with differnece
                if diff in dp[x]:
                    # any subsequences with length 2 and length 3 at x will be added to arithmentic
                    # sequefnces at i
                    dp[i][diff][2] += dp[x][diff][1]
                    dp[i][diff][2] += dp[x][diff][2]

                
                # there will always be a sequence of length 2 with diff (just value at i
                # and just value at x)
                dp[i][diff][1] += 1

                # if we have already used this difference in our arithmetic total
                # then we need to add the difference between how much we added and what it
                # is at now (basically just keeps track of arithimetic slices at ever differcne
                # at index)
                if diff in diffs_added:
                    arithmetic_seq += (dp[i][diff][2] - diffs_added[diff])
                    diffs_added[diff] = dp[i][diff][2]
                else:
                    arithmetic_seq += dp[i][diff][2]
                    diffs_added[diff] = dp[i][diff][2]
            
            total += arithmetic_seq


        return total
        
102100