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.


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 C++ implementation of above idea –

// 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)

    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 ( > 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;



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 or mail your article to 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.

GATE CS Corner    Company Wise Coding Practice

Please write to us at to report any issue with the above content.

Recommended Posts:

3.7 Average Difficulty : 3.7/5.0
Based on 36 vote(s)