Given a positive integer, find maximum integer possible by doing at-most K swap operations on its digits.

**Examples:**

Input:M = 254, K = 1Output:524 Swap 5 with 2 so number becomes 524Input:M = 254, K = 2Output:542 Swap 5 with 2 so number becomes 524 Swap 4 with 2 so number becomes 542Input: M = 68543, K = 1Output:86543 Swap 8 with 6 so number becomes 86543Input:M = 7599, K = 2Output:9975 Swap 9 with 5 so number becomes 7995 Swap 9 with 7 so number becomes 9975Input:M = 76543, K = 1Output:76543Explanation:No swap is required.Input:M = 129814999, K = 4Output:999984211 Swap 9 with 1 so number becomes 929814991 Swap 9 with 2 so number becomes 999814291 Swap 9 with 8 so number becomes 999914281 Swap 1 with 8 so number becomes 999984211

**Naive Solution:**

**Approach:** 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 maximum number. The process is repeated K times. The code can be further optimized, if the current digit is swapped with a digit less than the following digit.

**Algorithm: **

- Create a global variable which will store the maximum string or number.
- Define a recursive function that takes the string as number and value of k
- Run a nested loop, the outer loop from 0 to length of string -1 and inner loop from i+1 to end of string.
- Swap the ith and jth character and check if the string is now maximum and update the maximum string.
- Call the function recursively with parameters: string and k-1.
- Now again swap back the ith and jth character.

## C++

`// C++ program to find maximum ` `// integer possible by doing ` `// at-most K swap operations ` `// on its digits. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find maximum ` `// integer possible by ` `// doing at-most K swap ` `// operations on its digits ` `void` `findMaximumNum( ` ` ` `string str, ` `int` `k, string& max) ` `{ ` ` ` `// Return if no swaps left ` ` ` `if` `(k == 0) ` ` ` `return` `; ` ` ` ` ` `int` `n = str.length(); ` ` ` ` ` `// Consider every digit ` ` ` `for` `(` `int` `i = 0; i < n - 1; i++) { ` ` ` ` ` `// Compare it with all digits after it ` ` ` `for` `(` `int` `j = i + 1; j < n; j++) { ` ` ` `// if digit at position i ` ` ` `// is less than digit ` ` ` `// at position j, swap it ` ` ` `// and check for maximum ` ` ` `// number so far and recurse ` ` ` `// for remaining swaps ` ` ` `if` `(str[i] < str[j]) { ` ` ` `// swap str[i] with str[j] ` ` ` `swap(str[i], str[j]); ` ` ` ` ` `// If current num is more ` ` ` `// than maximum so far ` ` ` `if` `(str.compare(max) > 0) ` ` ` `max = str; ` ` ` ` ` `// recurse of the other k - 1 swaps ` ` ` `findMaximumNum(str, k - 1, max); ` ` ` ` ` `// Backtrack ` ` ` `swap(str[i], str[j]); ` ` ` `} ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string str = ` `"129814999"` `; ` ` ` ` ` `int` `k = 4; ` ` ` ` ` `string max = str; ` ` ` `findMaximumNum(str, k, max); ` ` ` ` ` `cout << max << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find maximum ` `# integer possible by doing at-most ` `# K swap operations on its digits. ` ` ` `# utility function to swap two ` `# characters of a string ` `def` `swap(string, i, j): ` ` ` ` ` `return` `(string[:i] ` `+` `string[j] ` `+` ` ` `string[i ` `+` `1` `:j] ` `+` ` ` `string[i] ` `+` `string[j ` `+` `1` `:]) ` ` ` `# function to find maximum integer ` `# possible by doing at-most K swap ` `# operations on its digits ` `def` `findMaximumNum(string, k, maxm): ` ` ` ` ` `# return if no swaps left ` ` ` `if` `k ` `=` `=` `0` `: ` ` ` `return` ` ` ` ` `n ` `=` `len` `(string) ` ` ` ` ` `# consider every digit ` ` ` `for` `i ` `in` `range` `(n ` `-` `1` `): ` ` ` ` ` `# and compare it with all digits after it ` ` ` `for` `j ` `in` `range` `(i ` `+` `1` `, n): ` ` ` ` ` `# if digit at position i is less than ` ` ` `# digit at position j, swap it and ` ` ` `# check for maximum number so far and ` ` ` `# recurse for remaining swaps ` ` ` `if` `string[i] < string[j]: ` ` ` ` ` `# swap string[i] with string[j] ` ` ` `string ` `=` `swap(string, i, j) ` ` ` ` ` `# If current num is more than ` ` ` `# maximum so far ` ` ` `if` `string > maxm[` `0` `]: ` ` ` `maxm[` `0` `] ` `=` `string ` ` ` ` ` `# recurse of the other k - 1 swaps ` ` ` `findMaximumNum(string, k ` `-` `1` `, maxm) ` ` ` ` ` `# backtrack ` ` ` `string ` `=` `swap(string, i, j) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `string ` `=` `"129814999"` ` ` `k ` `=` `4` ` ` `maxm ` `=` `[string] ` ` ` `findMaximumNum(string, k, maxm) ` ` ` `print` `(maxm[` `0` `]) ` ` ` `# This code is contributed ` `# by vibhu4agarwal ` |

*chevron_right*

*filter_none*

**Output:**

999984211

**Complexity Analysis:**

**Time Complexity:**O((n^2)^k).

For every recursive call n^2 recursive calls is generated until the value of k is 0. So total recursive calls are O((n^2)^k).**Space Complexity:**O(n).

This is the space required to store the output string.

__Efficient Solution__:

**Approach:** The above approach traverses the whole string at each recursive call which is highly inefficient and unnecessary. Also, pre-computing the maximum digit after the current at a recursive call avoids unnecessary exchanges with each digit. It can be observed that to make the maximum string, the maximum digit is shifted to the front. So, instead of trying all pairs, try only those pairs where one of the element is the maximum digit which is not yet swapped to the front.

*There is an improvement by 27580 microseconds for each test case*.

**Algorithm:**

- Create a global variable which will store the maximum string or number.
- Define a recursive function that takes the string as number, value of k, and current index.
- Find the index of maximum element in range current index to end.
- if the index of maximum element is not equal to current index then decrement value of k.
- Run a loop from current index to the end of array
- If the ith digit is equal to the maximum element
- Swap the ith and element at current index and check if the string is now maximum and update the maximum string.
- Call the function recursively with parameters: string and k.
- Now again swap back the ith and element at current index.

## C++

`// C++ program to find maximum ` `// integer possible by doing ` `// at-most K swap operations on ` `// its digits. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find maximum ` `// integer possible by ` `// doing at-most K swap operations ` `// on its digits ` `void` `findMaximumNum( ` ` ` `string str, ` `int` `k, ` ` ` `string& max, ` `int` `ctr) ` `{ ` ` ` `// return if no swaps left ` ` ` `if` `(k == 0) ` ` ` `return` `; ` ` ` ` ` `int` `n = str.length(); ` ` ` ` ` `// Consider every digit after ` ` ` `// the cur position ` ` ` `char` `maxm = str[ctr]; ` ` ` `for` `(` `int` `j = ctr + 1; j < n; j++) { ` ` ` `// Find maximum digit greater ` ` ` `// than at ctr among rest ` ` ` `if` `(maxm < str[j]) ` ` ` `maxm = str[j]; ` ` ` `} ` ` ` ` ` `// If maxm is not equal to k, ` ` ` `// decrement k ` ` ` `if` `(maxm != str[ctr]) ` ` ` `--k; ` ` ` ` ` `// search this maximum among the rest ` ` ` `for` `(` `int` `j = ctr; j < n; j++) { ` ` ` `// If digit equals maxm swap ` ` ` `// the digit with current ` ` ` `// digit and recurse for the rest ` ` ` `if` `(str[j] == maxm) { ` ` ` `// swap str[ctr] with str[j] ` ` ` `swap(str[ctr], str[j]); ` ` ` ` ` `// If current num is more than ` ` ` `// maximum so far ` ` ` `if` `(str.compare(max) > 0) ` ` ` `max = str; ` ` ` ` ` `// recurse other swaps after cur ` ` ` `findMaximumNum(str, k, max, ctr + 1); ` ` ` ` ` `// Backtrack ` ` ` `swap(str[ctr], str[j]); ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string str = ` `"129814999"` `; ` ` ` `int` `k = 4; ` ` ` ` ` `string max = str; ` ` ` `findMaximumNum(str, k, max, 0); ` ` ` ` ` `cout << max << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

999984211

**Complexity Analysis:**

**Time Complexity:**O(n^k).

For every recursive call n recursive calls is generated until the value of k is 0. So total recursive calls are O((n)^k).**Space Complexity:**O(n).

The space required to store the output string.

**Exercise:**

- Find minimum integer possible by doing at-least K swap operations on its digits.
- Find maximum/minimum integer possible by doing exactly K swap operations on its digits.

This article is contributed by **Aditya Goel**.If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find maximum possible stolen value from houses
- Largest even number possible by using one swap operation in given number
- Boggle (Find all possible words in a board of characters) | Set 1
- Find the row with maximum number of 1s
- Find the maximum repeating number in O(n) time and O(1) extra space
- Total number of possible Binary Search Trees and Binary Trees with n keys
- Count the number of possible triangles
- Sliding Window Maximum (Maximum of all subarrays of size k)
- Find the Number Occurring Odd Number of Times
- Find the smallest number whose digits multiply to a given number n
- Write a Program to Find the Maximum Depth or Height of a Tree
- Given an array arr[], find the maximum j - i such that arr[j] > arr[i]
- Find the maximum element in an array which is first increasing and then decreasing
- Find the maximum path sum between two leaves of a binary tree
- Find a pair with maximum product in array of Integers
- Find maximum average subarray of k length
- Find maximum of minimum for every window size in a given array
- Find maximum level sum in Binary Tree
- Count all possible paths from top left to bottom right of a mXn matrix
- Count all possible groups of size 2 or 3 that have sum as multiple of 3