Simple Solution
Code

# Code
```cpp []
class Solution {
public:
    int longestSubsequence(vector<int>& nums) {
        int ans = 1;
        int n = nums.size();
        vector<int> mp(301, -1); // Stores the last index of each number (values 1 to 300).
        vector<vector<int>> dp(10000, vector<int>(300, 1)); // dp[i][diff] stores the max length of subsequence ending at index i with difference diff.

        for (int i = n - 1; i >= 0; i--) {
            int val = nums[i];
            int tans = 1; // Temporary answer for the current index.

            for (int diff = 0; diff <= 299; diff++) {
                // Case 1: Adding 'diff' to the current value
                if (val + diff <= 300) {
                    int newVal = val + diff;
                    if (mp[newVal] != -1) { // Check if the new value exists
                        tans = max(tans, dp[mp[newVal]][diff] + 1); // Update tans based on dp of new value.
                    }
                }
                // Case 2: Subtracting 'diff' from the current value
                if (val - diff >= 1) {
                    int newVal = val - diff;
                    if (mp[newVal] != -1) { // Check if the new value exists
                        tans = max(tans, dp[mp[newVal]][diff] + 1); // Update tans based on dp of new value.
                    }
                }
                dp[i][diff] = tans; // Store the computed tans for current index and diff.
            }
            mp[val] = i; // Update the last occurrence of the current value.
            ans = max(ans, tans); // Update the overall answer.
        }
        return ans;
    }
};

auto init = []() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    return 'c';
}();

```
['C++']