Can you let me know where is my code wrong

After 2 swaps, the answer should be [5,4,1,2,3], and this what I am getting as output also, but I don’t understand how can the answer be [5,4,3,2,1 ] after 2 swaps.

@ashsingh
first swap 5 and 3 array becomes
5 4 1 2 3
second swap 1 and 3
5 4 3 2 1
hope its clear if yes dont forget to mark the doubt as resolved :smiley:

Hi, vatsal I have doubt that why are we swapping 3,1 but it should be 4 swapped with 4 to as it is the next largest element after 5 and we want to create the largest number possible.

@ashsingh you just have to make atmost k swaps to make the largest possible number and as you can see largest in atmost 2 swaps is 5 4 3 2 1

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.