O(1) spacec, O(n) time
IntuitionExploit the property that input numbers are between 0 & 300, and thus that the absolute diff is between 0 & 300.For every number (between 0 & 300), sto
# Intuition
Exploit the property that input numbers are between 0 & 300, and thus that the absolute diff is between 0 & 300.

For every number (between 0 & 300), store the longest subsequence at each diff (between 0 & 300).

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity: O(n), ignoring all the `300` steps work

- Space complexity: O(1)

# Code
```cpp []
class Solution {
public:
    int longestSubsequence(vector<int>& nums) {
        vector<vector<int>> subarray_longest_subseqs(301, vector<int>(302, 0));
        for (auto elA = 0; elA < nums.size(); elA++) {
            subarray_longest_subseqs[nums[elA]][301] = 1;
            vector<int> new_answers = subarray_longest_subseqs[nums[elA]];
            for (int elB = 0; elB < 301; elB++) {
                int diff = abs(nums[elA] - elB);
                new_answers[diff] = std::max(
                    new_answers[diff],
                    subarray_longest_subseqs[elB][diff] + 1
                );
            }
            subarray_longest_subseqs[nums[elA]] = new_answers;
            for (int idx = 300; idx >= 0; idx--) {
                subarray_longest_subseqs[nums[elA]][idx] = std::max(
                    subarray_longest_subseqs[nums[elA]][idx],
                    subarray_longest_subseqs[nums[elA]][idx + 1]
                );
            }
        }

        return std::accumulate(
            subarray_longest_subseqs.begin(), subarray_longest_subseqs.end(),
            0, [](auto accum, auto val){ return std::max(accum, val[0]); }
        );
    }
};

```
['C++']