Find Maximum number possible by doing at-most K swaps

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

Examples:

Input: M = 254, K = 1
Output: 524

Input: M = 254, K = 2
Output: 542

Input: M = 68543, K = 1 
Output: 86543

Input: M = 7599, K = 2
Output: 9975

Input: M = 76543, K = 1 
Output: 76543

Input: M = 129814999, K = 4
Output: 999984211



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. We repeat the process K times. The code can be further optimized if we swap only if current digit is less than the following digit.

Below is implementation of above idea –

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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++)
    {
       
        // and 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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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



Output:

999984211

The above code can further be optimized by stopping our search if all digits are already sorted in decreasing order. Please do share with us if you find more efficient ways to solve this problem.

Exercise :
1. Find minimum integer possible by doing at-least K swap operations on its digits.
2. 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.



My Personal Notes arrow_drop_up

Improved By : vibhu4agarwal