Easiest code || recursion+memo
Intuition\nThe goal of the code is to find the length of the longest subsequence in the given vector nums such that the sum of elements in the subsequence is eq
# Intuition\nThe goal of the code is to find the length of the longest subsequence in the given vector nums such that the sum of elements in the subsequence is equal to the given target.\n\n\n\n# Approach\nRecursive Function (f):\n\nThe function f is recursive and explores two possibilities at each step: taking the current element (nums[ind]) into the subsequence or not taking it.\nThe base case is when the target sum becomes 0. In this case, the length of the current subsequence is considered, and the function returns 0.\nIf the target is not 0, and if ind becomes negative (no more elements to consider), the function returns a very large negative value (-1e9) to indicate an invalid subsequence.\nMemoization:\n\nTo optimize the recursive calls, memoization is used. The dp array stores intermediate results for each combination of ind and target.\nIf the result for a specific ind and target is already computed, it is directly returned from the dp array, avoiding redundant calculations.\nChoosing Elements:\n\nAt each step, the algorithm considers two choices: taking the current element into the subsequence (take), or not taking it (not_take).\nIf the current element is less than or equal to the remaining target, the algorithm explores the possibility of taking it and increments the length of the subsequence.\nReturning the Result:\n\nThe final result is the maximum length of the subsequence obtained by considering or not considering each element in the nums array.\nIf the result is negative, it means there is no valid subsequence, and -1 is returned. Otherwise, the length of the longest subsequence is returned.\n\n# Complexity\n- Time complexity:\nTime complexity (TC): O(n * target)\nSpace complexity (SC): O(n * target)\n\n# Code\n```\nclass Solution {\n    int f(int ind,vector<int>& nums, int target,vector<vector<int>>&dp){\n        if(target==0){\n           return 0;\n        }\n        if(ind<0 && target!=0)return -1e9;\n        if(dp[ind][target]!=-1 )return dp[ind][target];\n        int take=-1e9;;\n        if(nums[ind]<=target){\n  take=1+f(ind-1,nums,target-nums[ind],dp);\n        }\n        int not_take=f(ind-1,nums,target,dp);\n        return dp[ind][target]=max(take,not_take);\n    }\npublic:\n    int lengthOfLongestSubsequence(vector<int>& nums, int target) {\n        int n=nums.size();\n       vector<vector<int>>dp(n,vector<int>(target+1,-1));\n        //memset(dp,-1,sizeof(dp));\n        int ans= f(n-1 ,nums,target,dp);\nif(ans<0)return -1;\nelse return ans;\n    }\n};\n```
['Dynamic Programming', 'Recursion', 'Memoization', 'C++']