


1. The input may have 0 before the most significant digit, [0,3,5,7] is a valid input and it represents number 357.
2. Digits in the number can be repeated, i.e [3, 3, 4, 4] is a valid input and it represents the number 3344.
The first line of input contains a single integer 'T', representing the number of test cases.
Then the test cases follow.
The first line of each test case contains two space-separated integers 'N' and 'K', denoting the size of the array and the number of swaps permitted respectively.
The second line contains 'N' space-separated integers representing the digits as described in the problem statement.
For each test case, print the space-separated digits of the maximum number possible.
You don’t have to print anything, it has already been taken care of. Just implement the given function.
1<= T <=10
2 <= N <= 10
0 <= K <= min(N,5)
0 <= NUM[i] <= 9
Time Limit: 1 sec
The idea is to generate all the possible numbers by trying all the possible combinations of indices at most ‘K’ times. We will do this task with a recursive function and each time we swap two indices we will decrease the value of ‘K’ by 1. So base case is when ‘K’ is equal to zero and each time when we swap indices we will check whether the current number is the maximum possible or not.
Here is the algorithm :
We can improve the time complexity of the previous - discussed approach by making a slight modification. So to get the maximum number a digit must be swapped only with the maximum digit available on its right-hand side and which is also greater than the current digit.
We will do this task with a recursive function and each time we swap two indices we will decrease the value of ‘K’ by 1. So, the base case will be when ‘K’ is equal to zero and each time when we swap indices we will check whether the current number is the maximum possible or not.
Here is the algorithm :