RECURSION TO MEMOIZATION (INTUTIBLE CODE)
\n# RECURSION \n# Code\n\nclass Solution {\npublic:\n    int solve(vector<int>&nums,int target,int i)\n    {\n        if(target==0){\n            return 0;\n   
\n# RECURSION \n# Code\n```\nclass Solution {\npublic:\n    int solve(vector<int>&nums,int target,int i)\n    {\n        if(target==0){\n            return 0;\n        }\n        if(target<0 || i>=nums.size())return INT_MIN;\n        \n        int take=1+solve(nums,target-nums[i],i+1);\n        int not_take=solve(nums,target,i+1);\n        \n        return max(take,not_take);\n        \n    }\n    int lengthOfLongestSubsequence(vector<int>& nums, int target) {\n       \n       int ans=solve(nums,target,0);\n       return ans<1?-1:ans;   \n    }\n};\n```\n#  MEMOIZATION\n# Code\n```\nclass Solution {\npublic:\n    int dp[1001][1001];\n    int solve(vector<int>&nums,int target,int i)\n    {\n        if(target==0){\n            return 0;\n        }\n        if(target<0 || i>=nums.size())return INT_MIN;\n        if(dp[i][target]!=-1)\n        {\n            return dp[i][target];\n        }\n        int take=1+solve(nums,target-nums[i],i+1);\n        int not_take=solve(nums,target,i+1);\n        \n        return dp[i][target]=max(take,not_take);\n        \n    }\n    int lengthOfLongestSubsequence(vector<int>& nums, int target) {\n       int n=nums.size();\n       memset(dp,-1,sizeof(dp));\n       int ans=solve(nums,target,0);\n       return ans<1?-1:ans;   \n    }\n};\n```
['C++']