Tried using segment tree but that gave a tle
IntuitionApproachComplexity
Time complexity:O(n*300)

Space complexity:O(300*300)
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

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

# Complexity
- Time complexity:O(n*300)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:O(300*300)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
#define ll long long
class SegmentTree {
    vector<ll> sgtree;

public:
    SegmentTree(ll n) {
        sgtree.resize(4 * n + 1, 0); // Initialize segment tree with 0
    }

    ll query(ll index, ll low, ll high, ll l, ll h) {
        if (low > h || l > high)
            return 0; // Return 0 for non-overlapping range

        if (l <= low && high <= h)
            return sgtree[index];

        ll mid = (low + high) / 2;

        return max(query(2 * index + 1, low, mid, l, h),
                   query(2 * index + 2, mid + 1, high, l, h));
    }

    void update(ll index, ll low, ll high, ll i, ll val) {
        if (low == high) {
            sgtree[index] = max(sgtree[index], val);
            return;
        }

        ll mid = (low + high) / 2;

        if (i <= mid)
            update(2 * index + 1, low, mid, i, val);
        else
            update(2 * index + 2, mid + 1, high, i, val);

        sgtree[index] = max(sgtree[2 * index + 1], sgtree[2 * index + 2]);
    }
};

class Solution {
public:
    int longestSubsequence(vector<int>& nums) {

        // if (nums.size() < 2)
        //     return nums.size();

         vector<vector<ll>> arrays(301, vector<ll>(301, 1));
        // vector<SegmentTree> sgs(301, SegmentTree(301));
        unordered_set<ll> st;

        ll n = nums.size();
        ll ans = 0;

        for (ll j = 0; j < n; j++) {
            ll x = nums[j];
            for (auto i : st) {
                auto diff = abs(x - i);
                auto q = arrays[i][diff];
                ll len = q + 1;

                arrays[x][diff] = max(arrays[x][diff], len);

                ans = max(ans, len);
            }
            for (ll k = 299; k>=0; k--) {
                arrays[x][k]=max(arrays[x][k],arrays[x][k+1]);
            }
            st.insert(x);
        }

        return ans;
        return 0;
    }
};

```
['Array', 'Dynamic Programming', 'Segment Tree', 'C++']