2d dp

```java []
class Solution {
    public int longestSubsequence(int[] nums) {
        int[][]dp=new int[302][302];
        for(int i=nums.length-1;i>=0;i--){
            int x=nums[i];
            for(int j=1;j<302;j++){
               dp[x][Math.abs(j-x)]=Math.max(dp[x][Math.abs(j-x)],dp[j][Math.abs(j-x)]+1);
            }
            
            for (int j = 1; j <= 300; ++j){
                dp[x][j] = Math.max(dp[x][j], dp[x][j - 1]);
            }
        }  
        
        int ans = -1000000;
        for (int i = 0; i <= 301; ++i){
            for (int j = 0; j <= 301; ++j){
                ans =Math.max(ans, dp[i][j]);
            }
        }
        return ans;
    }
}
```
['Java']