C++ dp (space optimized)
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n$len[i]$ stores the longest subsequence whose sum is $i$. Traverse the ar
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n$len[i]$ stores the longest subsequence whose sum is $i$. Traverse the array from the left to the right and, for each possible sum, update $len$. \n\n# Complexity\n- Time complexity: $$O(n \\cdot  target)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(target)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int lengthOfLongestSubsequence(vector<int>& nums, int target) {\n        int n = nums.size();\n        int ans = -1;\n        vector<int> len(target+1, -1);\n        len[0] = 0;\n\n        for(int i=0; i<n; i++){\n            for(int sum=target; sum>=nums[i]; sum--){\n                if(len[sum-nums[i]] != -1){\n                    len[sum] = max(len[sum], 1 + len[sum-nums[i]]);\n                }\n            }\n        }\n\n\n\n        return len[target];\n    }\n};\n```
['C++']