Aditya Verma Approach
\n# Complexity\n- Time complexity:O(n^2)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:O(n^2)\n Add your space complexity here, e.g. O(n) \n
\n# Complexity\n- Time complexity:O(n^2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(n^2)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int lengthOfLongestSubsequence(vector<int>& arr, int sum) {\n        int n = size(arr);\n        vector<vector<int>> dp(n+1,vector<int> (sum+1,-1));\n\n        for(int i = 0;i<=sum;i++){\n            dp[0][i]=INT_MIN+1;\n        }\n\n        for(int i = 0;i<=n;i++){\n            dp[i][0] =1;\n        }\n\n        for(int i =1;i<=n;i++){\n            for(int j = 1;j<=sum;j++){\n                if(j - arr[i-1] >=0){\n                    dp[i][j] = max(1+dp[i-1][j-arr[i-1]],dp[i-1][j]);\n                }\n                else{\n                    dp[i][j] = dp[i-1][j];\n                }\n            }\n        }\n\n        return dp[n][sum]>0 ? dp[n][sum]-1 : -1; \n    }\n};\n```
['Array', 'Dynamic Programming', 'C++']