
‘NUM’ = “5432”, ‘N’ = 4 and ‘K’ = 4.
We can perform the following ‘4’ swaps to obtain the minimum integer from “5432”:

Thus, you should return the string “2453” as the answer.
1. ‘NUM’ doesn’t have leading zeroes.
2. The resultant minimum integer can have leading zeroes.
The first line of input contains an integer ‘T’ which denotes the number of test cases. Then, the ‘T’ test cases follow.
The first line of each test case contains two integers, ‘N’ and ‘K’, denoting the number of digits in ‘NUM’ and the maximum allowed swaps.
The second line of each test case contains the string ‘NUM’.
For every test case, return the string representing the minimum integer obtained by performing at most ‘K’ swaps.
You do not need to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N <= 500
1 <= k <= 10^4
The string ‘NUM’ contains only digits in the range [0, 9].
Time limit: 1 sec
The idea behind the greedy approach:
We need two levels of loops, the outer loop to traverse the integer ‘NUM’, and the inner (nested) loop to find the smallest leftmost digit among the next ‘K’ digits and swap the digits in between.
Algorithm:
We use ten stacks ‘POS[10]’ (using array implementation) to store the indexes of digits from ‘0’ to ‘9’. The stack ‘POS[i]’ stores the indexes of the digit ‘i’ in descending order where the back of the array/top of the stack is the leftmost (smallest) index. This can be achieved by traversing ‘NUM’ in reverse, and for every index ‘i’ push ‘i’ into ‘POS[NUM[i]]’.
Prerequisite: Lower bound. To count the number of elements smaller or equal in a sorted array.
The number of digits ‘i’ to the left of an index ‘ID’ in ‘NUM’ can be found by doing a binary search to count the number of elements in ‘POS[i]’, which are smaller than ‘ID’. If we do this for all the digits from ‘0’ to ‘9’, we can find the number of digits to the left of index ‘ID’.
Create an empty string ‘RES’ to store the minimum possible number. As per the previous approach, the most significant (leftmost) digit should be the minimum of the first ‘K + 1’ digits (given that ‘K + 1 <= N’). So, starting to traverse ‘POS’ from ‘0’ to ‘9’ until we find a digit ‘D’ that has not more than ‘K’ digits to its left. When we find ‘D’, append ‘D’ to the ‘res’, mark the location of ‘D’ in ‘NUM’, update the value of ‘K’, and pop ‘POS[D]’. If all the stacks become empty before performing the ‘K’ swaps, then ‘RES’ stores the answer.
Otherwise, after the ‘K’ swaps are exhausted, append the unmarked digits from ‘0’ to ‘N - 1’ for ‘NUM’ to ‘RES’ (as we always swap the digits from right to left the order of unmarked digits in the final answer and ‘NUM’ is the same).
Algorithm:
Prerequisite: The segment tree. To perform the following two operations on an array:
Similar to the previous approach, creating ten stacks ‘POS[10]’ (using array implementation) to store the indexes of digits from ‘0’ to ‘9’. The stack ‘POS[i]’ stores the indexes of the digit ‘i’ in descending order where the back of the array/top of the stack is the leftmost (smallest) index.
Create a segment tree for an array of size ‘N’ with each element set to ‘0’ and use it to count the right shifts in digits. Whenever we move a digit from index ‘ID’ to ‘i’, increment the indexes from ‘0’ to ‘ID - 1’ by ‘1’. As after performing the ‘ID - i - 1’ swaps, the digits from ‘i’ to ‘ID - 1’ move right by one position. We increment the indexes from ‘0’ because:
The number of digits from an index ‘i’ to an index ‘ID’ in is equal to = ‘ID + QUERY(ID) - i’. Here, ‘QUERY(ID)’ will return the number of positions that ‘ID’ has been shifted to the right.
Create an empty string ‘RES’ to store the minimum possible number. As per the previous approach, the most significant (leftmost) digit should be the minimum of the first ‘K + 1’ digits (given that ‘K + 1 <= N’). So, starting to traverse ‘POS’ from ‘0’ to ‘9’ until we find a digit ‘D’ that has not more than ‘K’ digits to its left. When we find ‘D’, append ‘D’ to the ‘res’ and mark the location of ‘D’ in ‘NUM’. Increase the indexes from ‘[0 to (the index of ‘D’ - 1)] by ‘1’, pop ‘POS[D]’, and update the value of ‘K’. If all the stacks become empty before performing the ‘K’ swaps, then ‘RES’ stores the answer.
Otherwise, after the ‘K’ swaps are exhausted, append the unmarked digits from ‘0’ to ‘N - 1’ for ‘NUM’ to ‘RES’ (as we always swap the digits from right to left the order of unmarked digits in the final answer and ‘NUM’ is the same).
Algorithm: