Shekhar is a bomb defusal specialist. He once encountered a bomb that can be defused only by a secret code. He is given a number N and a number K. And he is also given permutation of first N natural numbers . The defusal code is the largest permutation possible by doing exactly K swaps among a pair of the given permutation. Help him to find the final permutation.
How to approach this
You have to make the largest permutation possible in k swaps, so all you have to do is to place maximum at first index and second maximum at second index and so on.
If you have a maximum at first index then go for the second maximum and if 2nd index does not contain the second maximum then search for the second maximum in [3…n] in the array and swap the second maximum with the 2nd index value. Similarly, at end of k swaps, you will have your maximum possible permutation in k swaps.
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.