Simple Solution Using DP+ Memorization Easy to Understand
Intuition\n Describe your first thoughts on how to solve this problem. \nUse pick and not pick logic and handle base case \n# Approach\n Describe your approach 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUse pick and not pick logic and handle base case \n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe have give array and a target we have to find longest subsequnce length that sum to target\nour first base case if array contain only one value then we check that value only and return\nelse \nwe use pick and not pick logic  we can only pick if target>=nums other wise our target will become negative [index] where index is our current positon\nif our target become 0 means we have found the subsequnce we update maxi \n\n\nPick:\nwhen we pick element we inc the count 1 in length and target is reduce by our picked element \nwe dec the index by one \n\n\nNot pick:\nwe dec the index by one and target remain same because not pick \n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n*t) \nwhere n is number of element and t is target\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(n*t) \nwhere n is number of element and t is target\n# Code\n```\nclass Solution \n{\npublic:\n    int solve(vector<int>& ans, int target, int index, vector<vector<int>>& dp) \n    {\n        if (target == 0) \n        {\n            return 0;\n        }\n        if (index < 0) \n        {\n            return -1e9;\n        }\n        if (dp[index][target] != -1) \n        {\n            return dp[index][target];\n        }\n\n        int take = -1e9;\n        if (ans[index] <= target) \n        {\n            take = 1 + solve(ans, target - ans[index], index - 1, dp);\n        }\n        int not_take = solve(ans, target, index - 1, dp);\n\n        return dp[index][target] = max(take, not_take);\n    }\n\n    int lengthOfLongestSubsequence(vector<int>& ans, int target) \n    {\n        if (ans.size() == 1) \n        {\n            if (ans[0] == target) \n            {\n                return 1;\n            }\n            else \n            {\n                return -1;\n            }\n        }\n        int index = ans.size() - 1;\n        vector<vector<int>> dp(ans.size(), vector<int>(target + 1, -1));\n        int result = solve(ans, target, index, dp);\n        return result < 0 ? -1 : result;\n    }\n};\n\n```
['C++']