✅ C++ sol. using TOP-DOWN (RECURSIVE) DP
Code


# Code
```cpp []
class Solution {
public:
    int dp[1001][1001];
    int fun(vector<int>&v , int in , int t){
        if(t == 0){
            return 0;
        }

        if(in >= v.size() || t < 0){
            return -1000 ;
        }

        if(dp[in][t] != -1){
            return dp[in][t];
        }

        int take = 1 + fun(v , in+1 , t - v[in]);
        int ntake = fun(v , in+1 , t);
        return dp[in][t] = max(take , ntake);
    }
    int lengthOfLongestSubsequence(vector<int>& nums, int target) {
        memset(dp , -1 , sizeof(dp));
        int ans = fun(nums , 0 , target) ;
        return ans > 0 ? ans : -1;
    }
};
```
['Array', 'Dynamic Programming', 'Recursion', 'Memoization', 'C++']