Sum it up problem

LINK TO MY CODE

I HAVE UNDERSTOOD THE CONCEPTUAL ERROR WITH MY CODE ( i-e it is not always necessary that the condition arr[si]<=t works)(as given in the code).

BUT I AM UNABLE TO FIGURE OUT HOW TO IMPLEMENT IT AND CORRECT IT . KINDLY SUGGEST THE CHANGE THAT IS TO BE BROUGHT TO THE CODE .

@ChiragJindal7 have you tried first sorting the array and then approaching the question? I do not understand why have you made it so complicated.
You can start from i = 0, and binary search for an element that can give the required sum, if it does not exist do not include the ith element in the answer, if it is found then push the pair in the vector. This way the answer will always be in ascending order.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

@ChiragJindal7
The major problem with your code is that you are using a common global vector to store your results and cleaning it afterwards. Using global variables/objects across recursion is complicated and not at all adviced. You have used it and that is where your mistake lies. After you reach a valid configuration , you clear the vector making all other configurations using the same partial vector invalid.
Instead , pass this vector as an argument in your recursion function. That will sort your code out to a great extent. Also , the if condition where you check arr[si] <= t is not a problem… The problem is the else part. There should not be a else clause. The recursive call that you have made in else part should be made always , whatever the condition.
Also , the condition where you check t==0 should be the base case instead of second.
Make these changes and try again .

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.