Subset Sum Variation DP , Simple recursion + Memoization 👋🏻
\n# Code\n\nclass Solution {\npublic:\n    vector<int> arr;\n    int n ;\n    int dp[1020][1020];\n    int rec(int lev , int taken ) {\n            if (taken ==
\n# Code\n```\nclass Solution {\npublic:\n    vector<int> arr;\n    int n ;\n    int dp[1020][1020];\n    int rec(int lev , int taken ) {\n            if (taken == 0 ) return  0 ;\n            if (lev >= arr.size() or taken < 0) return -1e9;\n            if(dp[lev][taken]!=-1) return dp[lev][taken];\n            return dp[lev][taken]=max(rec(lev + 1 , taken ), 1 + rec(lev + 1 , taken - arr[lev] ));\n    }\n    int lengthOfLongestSubsequence(vector<int>& nums, int target) {\n        arr = nums;\n        memset(dp,-1,sizeof(dp));\n        n = arr.size();\n        if(n==1 and arr[0]==target) return 1 ;\n        int ans = rec(0,target) ;\n\n        return ans > 0?ans : -1 ;\n    }\n};\n```
['C++']