☕ Java solution
![Screenshot_4.png](https://assets.leetcode.com/users/images/9fed3e7f-3f37-49ee-bbda-fb17b971cac5_1736062656.6429799.png)

```java []
class Solution {
    public int longestSubsequence(int[] nums) {
        int[] seen = new int[301]; 
        int[][] dp = new int[301][301]; 
        int maxLength = 0;

        for (int num : nums) {
            for (int prev = 300; prev >= 0; prev--) {
                if (seen[prev] == 1) {
                    int diff = Math.abs(num - prev);
                    dp[num][diff] = Math.max(dp[num][diff], dp[prev][diff] + 1);
                }
            }

            for (int diff = 299; diff >= 0; diff--) {
                dp[num][diff] = Math.max(dp[num][diff], dp[num][diff + 1]);
            }

            maxLength = Math.max(maxLength, dp[num][0]);
            seen[num] = 1; 
        }

        return maxLength + 1; 
    }
}
```
['Java']