# Find minimum number possible by doing at-most K swaps

Given a positive integer, find minimum number possible by doing at-most K swap operations upon its digits.

For example,

Input: S = 934651, K = 1
Output: 134659

Input: S = 934651, K = 2
Output: 134569

Input: S = 52341, K = 2
Output: 12345 (Only 1 swap needed)

Input: S = 12345, K = 2
Output: 12345 (no change as all digits are already sorted in increasing order)

We can use backtracking to solve this problem. The idea is to consider every digit and swap it with digits following it one at a time and see if it leads to the minimum number. The process is repeated K times.

## C++

Output:

The minimum number formed by doing at-least 2 swaps is 134569

## Java

Output:

The minimum number formed by doing at-least 2 swaps is 134569     (1 votes, average: 5.00 out of 5) Loading...

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂   