Simple C++ DP solution✅ | Beginner friendly | Tabulation 
Complexity

Time complexity: O(N*300)


Space complexity:  O(N*300) + O(600)

Code
# Complexity
- Time complexity: O(N*300)

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

# Code
```cpp []
class Solution {
public:
    int longestSubsequence(vector<int>& nums) {
        int n = nums.size();
        vector<int> last_occurence(601,-1); //stores the last occurence of the required number
        int dp[10000][301]; // dp[i][j] cintains the length of longest subsequence ending at i  such that absolute diff between numbers at last and 2nd last position in the subsequence is j
        
        for(int i=0; i<10000; i++){
            for(int j=0; j<=300; j++){
                dp[i][j] = 1; //initialising each value to 1
            }
        }
        last_occurence[nums[0]] = 0;
        int ans = 1;
        for(int i=1; i<n; i++){
            for(int diff=299; diff>=0; diff--){
                int no_greater_by_diff = nums[i] + diff;
                if(no_greater_by_diff>=0 && last_occurence[no_greater_by_diff] != -1){
                    int idx = last_occurence[no_greater_by_diff];
                    dp[i][diff] = max(dp[i][diff], 1 + dp[idx][diff]);
                }
                int no_smaller_by_diff = nums[i] - diff;
                if(no_smaller_by_diff>=0 && last_occurence[no_smaller_by_diff] != -1){
                    int idx = last_occurence[no_smaller_by_diff];
                    dp[i][diff] = max(dp[i][diff], 1 + dp[idx][diff]);
                }
                dp[i][diff] = max(dp[i][diff], dp[i][diff+1]); 
            }
            last_occurence[nums[i]] = i;
            ans = max(ans, dp[i][0]);
        }
        return ans;
    }
};
```
['Array', 'Hash Table', 'Dynamic Programming', 'C++']