C++ SIMPLE
IntuitionThe goal is to find the length of the longest subsequence of the array nums such that the absolute difference between consecutive elements in the subse
# Intuition
 The goal is to find the length of the longest subsequence of the array nums such that the absolute difference between consecutive elements in the subsequence is constant. This type of problem is solvable using dynamic programming (DP), as we can build the solution incrementally by calculating the longest valid subsequence ending at each position.

# Approach
Pre-computation:

Calculate the range m, which is the difference between the maximum and minimum elements of the array nums, plus one. This is used to limit the possible differences (k) between consecutive elements of the subsequence.
Dynamic Programming:

Use a 2D DP table dp[j][k], where:
j is the index of the current number in the array.
k is the possible difference between consecutive elements.
dp[j][k] stores the length of the longest subsequence ending at nums[j] with difference k.
Optimization with maxi:

Maintain a 2D auxiliary array maxi[601][m]:
maxi[x][k] stores the maximum length of a subsequence with difference k and last element as x.
This helps in quickly updating the DP table for future numbers.
Core Logic:

For each element nums[j], iterate through all possible differences k from 0 to m-1.
Update dp[j][k] using:
The previous maxi values for nums[j] - k and nums[j] + k.
Ensure values are valid (i.e., index bounds are respected).
Update the corresponding entry in maxi to reflect the current maximum subsequence length for the element nums[j].
Post-update:

Perform a right-to-left sweep for each nums[j] to ensure that maxi[nums[j]][t] holds the maximum length across all possible t >= k.

# 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 []
class Solution {
public:
    int longestSubsequence(vector<int>& nums) {
        
        int n = nums.size();
        int m = (*max_element(nums.begin(),nums.end())) - (*min_element(nums.begin(),nums.end())) + 1;
       
        int dp[n+1][m];
        int maxi[601][m];
       
        int ans = 0;

        memset(dp,0,sizeof(dp));
        memset(maxi,0,sizeof(maxi));
       

        
            for(int j=0;j<n;j++)
            {
                for(int k = 0; k<m ;k++)
                {
                   dp[j][k] = max(dp[j][k] , max(nums[j]<k?0:maxi[nums[j]-k][k],maxi[nums[j]+k][k])+1);
                   maxi[nums[j]][k]= dp[j][k];
                   ans = max(ans,dp[j][k]);
                }

                for(int t=m-2;t>=0;t--)
                {
                    maxi[nums[j]][t] = max(maxi[nums[j]][t],maxi[nums[j]][t+1]);
                }
            }

            return ans;
        
    }
};
```
['C++']