Skip to content
Related Articles

Related Articles

Maximize a number considering permutations with values smaller than limit

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 23 Jun, 2022

Given two numbers N and M. Construct maximal number by permuting (changing order) the digits of N, not exceeding M. 
Note: It is allowed to leave N as it is.
Examples: 
 

Input : N = 123, M = 222 
Output : 213 
There are total 3! permutations possible for N = 123, But the only permutation that satisfies the given condition is 213. Similarly, In example 2, there are total 4! permutations possible for N = 3921, But the only permutation that satisfies the given condition is 9321.
Input : N = 3921, M = 10000 
Output : 9321 
 

 

Approach: Let’s construct the answer digit by digit starting from the leftmost. We are asked to build a lexicographically maximal answer. So in this order, we should choose the greatest digit on each step. The approach is to iterate over all possible digits starting from the greatest. For each digit check if it’s possible to put it in this position and compare the resulting number with the number M. If it comes less than or equal to the value of M, then proceed to the next digit.
Below is the CPP implementation of the above approach: 
 

CPP




// CPP program to Maximize the given number.
#include <bits/stdc++.h>
 
using namespace std;
 
// Function to maximize the number N with
// limit as M.
string maximizeNumber(string N, int M)
{
    // Sorting the digits of the
    // number in increasing order.
    sort(N.begin(), N.end());
 
    for (int i = 0; i < N.size(); i++) {
        for (int j = i + 1; j < N.size(); j++) {
 
            // Copying the string into another
            // temp string.
            string t = N;
 
            // Swapping the j-th char(digit)
            // with i-th char(digit)
            swap(t[j], t[i]);
 
            // Sorting the temp string
            // from i-th pos to end.
            sort(t.begin() + i + 1, t.end());
 
            // Checking if the string t is
            // greater than string N and less
            // than or equal to the number M.
            if (stoll(t) > stoll(N) && stoll(t) <= M)
 
                // If yes then, we will permanently
                // swap the i-th char(or digit)
                // with j-th char(digit).
                swap(N[i], N[j]);
        }
    }
 
    // Returns the maximized number.
    return N;
}
 
// Driver function
int main()
{
    string N = "123";
    int M = 222;
    cout << maximizeNumber(N, M);
    return 0;
}
// This code is contributed by KaaL-EL.

Java




/*package whatever //do not write package name here */
 
import java.io.*;
import java.util.Arrays;
 
class GFG {
    // Java has no built-in swap function.
    public static String swap(String str, int i, int j)
    {
        char ch[] = str.toCharArray();
        char temp = ch[i];
        ch[i] = ch[j];
        ch[j] = temp;
        return new String(ch);
    }
 
    // Since STRINGS are immutable in Java, first we have to
    // convert it to a character array in order to sort.
    public static String
    sortString(String string, int s_index, int e_index)
    {
        char tempArray[] = string.toCharArray();
        // Sorting temp array using
        Arrays.sort(tempArray, s_index, e_index);
        // returning the new sorted string
        return new String(tempArray);
    }
 
    public static String maximizeNumber(String N, int M)
    {
        // Sorting the digits of the
        // number in increasing order.
        N = sortString(N, 0, N.length());
 
        for (int i = 0; i < N.length(); i++) {
            for (int j = i + 1; j < N.length(); j++) {
 
                // Copying the string into another
                // temp string.
                String t = N;
 
                // Swapping the j-th char(digit)
                // with i-th char(digit)
                t = swap(t, j, i);
 
                // Sorting the temp string
                // from i-th pos to end.
                t = sortString(t, i + 1, t.length());
 
                // Checking if the string t is
                // greater than string N and less
                // than or equal to the number M.
                if (Long.parseLong(t) > Long.parseLong(N)
                    && Long.parseLong(t) <= M)
 
                    // If yes then, we will permanently
                    // swap the i-th char(or digit)
                    // with j-th char(digit).
                    N = swap(N, i, j);
            }
        }
 
        // Returns the maximized number.
        return N;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String N = "123";
        int M = 222;
        System.out.println(maximizeNumber(N, M));
    }
}
//This code is contributed by KaaL-EL.

Output: 
 

213

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!