<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 {
#define ll long long
public:
    int numberOfArithmeticSlices(vector<int>& A) {
        // dp[i] = no. of subsequences ending at index "i"
        // dp[i][j] = no. of subsequences ending at index "i"
        // such that the common diff is j
        // int n = nums.size();
        // key = i, value = j
        // <index, <diff, max_length>>

        // int n = nums.size();
        // dp[i] = length of longest subseq such that last index of that seq is
        // nums[i]
        // dp[i][j] = length of longest subseq such that last index of that seq 
        // is nums[i] and common diff of that AP was "j"

        // you can't directly solve this question using 2D dp array
        // confusion is how to make base case of store only when the length is 
        // >= 3.

         ll n = A.size();
        if(n < 3) return 0;
        // map of index vs ( map of diff vs count ) i.e count of subseq with diff = diff ending at index 
        unordered_map<ll, map<ll, ll> > dp;  
        ll ans = 0;
        for(ll i = 1; i < n; i++)
        {
            for(ll j = i - 1; j >= 0; j--)
            {
                ll diff = (ll)A[i] - (ll)A[j];
                
                if(dp[j].find(diff) != dp[j].end())
                {
                    ans += dp[j][diff];    // this will always add subsequence of length >=3 
                    dp[i][diff] += dp[j][diff];   // All of j to be included (i.e arr[i] is appended to all subseq of j)
                }
                dp[i][diff] += 1;   // +1 for the subsequence arr[j],arr[i]
                // cout<<"i: "<<i<<" diff: "<<diff<<" val: "<<dp[i][diff]<<" ";
            }
            // cout<<endl;
        }
        return ans;

    }
};
128035