Please guide me on how to optimize recursive solution
Can we optimise a recursive solution. I am getting TLE as this has huge time complexity.\n\ncpp\nclass Solution {\npublic:\n\tvoid solve(int ind, long long sum,
Can we optimise a recursive solution. I am getting TLE as this has huge time complexity.\n\n```cpp\nclass Solution {\npublic:\n\tvoid solve(int ind, long long sum, vector<int> &nums, vector<long long> &ans, int n, int k) {\n\t\tif(ind < 0) {\n\t\t\tans.push_back(sum);\n\t\t\treturn;\n\t\t}\n\n\t\tsolve(ind-1, sum + nums[ind], nums, ans, n, k);    // take\n\t\tsolve(ind-1, sum, nums, ans, n, k);    // notake\n\t}\n\n    long long kSum(vector<int>& nums, int k) {\n\t\tint n = nums.size();\n\t\tvector<long long> ans;\n\n\t\tsolve(n-1, 0, nums, ans, n, k);\n\t\tsort(ans.rbegin(), ans.rend());\n\n\t\treturn ans[k-1];\n    }\n};\n```\n\nLet me know how can we optimise it. Thanks in advance.
['Dynamic Programming', 'Recursion']