C++ Tabulation DP
Connect with me on LinkedIn: https://www.linkedin.com/in/abhay5349singh/\n\nApproach:\n each element has 2 choices: contribute to target sum or not\n for an ele
**Connect with me on LinkedIn**: https://www.linkedin.com/in/abhay5349singh/\n\n**Approach:**\n* each element has 2 choices: contribute to target sum or not\n* for an element to contribute, its value should be <= remaining-sum\n\n```\nclass Solution {\npublic:\n    int lengthOfLongestSubsequence(vector<int>& nums, int sum) {\n        int n=nums.size();\n        vector<vector<int>> dp(n+1, vector<int> (sum+1,-1));\n        \n        for(int i=0;i<n+1;i++) dp[i][0]=0; // longest subsequence when target sum = 0\n        \n        for(int i=1;i<n+1;i++){\n            for(int j=1;j<sum+1;j++){\n\t\t\t\t// achieving sum = j for elements [0,i]\n                dp[i][j] = dp[i-1][j]; \n\t\t\n\t\t\t\t// if element can contribute to sum\n                if(j >= nums[i-1] && dp[i-1][j-nums[i-1]]!=-1) dp[i][j] = max(1+dp[i-1][j-nums[i-1]], dp[i][j]);\n            }\n        }\n        \n        return (dp[n][sum]);\n    }\n};\n```\n\n**Do upvote if it helps :)**
[]