VOOZH about

URL: https://www.geeksforgeeks.org/dsa/maximize-a-number-considering-permutations-with-values-smaller-than-limit/

⇱ Maximize permutations - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Maximize permutations

Last Updated : 2 Jun, 2026

Given two strings n and m representing non-negative integers, find the largest number that can be formed by rearranging the digits of n such that the resulting number is less than or equal to m.

  • Each digit of n must be used exactly once, and the resulting number must not contain leading zeros.
  • If no such arrangement is possible, return "-1".
  • The answer should be returned as a string.

Examples:

Input: n = "123", m = "222"
Output: "213"
Explanation: The permutations of "123" are "123", "132", "213", "231", "312", and "321". Among these, "123", "132", and "213" are less than or equal to "222". Therefore, the answer is "213".

Input: n = "3921", m = "10000"
Output: "9321"
Explanation: Since n has fewer digits than m, every permutation of "3921" is less than "10000". The maximum permutation is "9321".

[Naive Approach] Generate All Permutations - O(|n|!) Time and O(n) Space

The idea is to generate all possible permutations of the digits of n using recursion (backtracking). For each permutation, we check whether it is valid, i.e., it has no leading zeros and is less than or equal to m. Among all valid permutations, we maintain the maximum one using a global variable.


Output
213

[Expected Approach] Greedy Digit Construction - O(|n|log|n|) Time and O(1) Space

Instead of generating all permutations of the digits of n, we build the answer greedily from left to right. The key idea is to always try to match the current digit of m using available digits from n. If at some position we cannot exactly match m, we try to place the largest possible smaller digit at that position and then maximize the remaining suffix by arranging all remaining digits in descending order.

Consider: n = "123", m = "222"

Step 1: We first store the frequency of digits present in n.

  • freq[1] = 1
  • freq[2] = 1
  • freq[3] = 1

Step 2: Start matching with m

We try to build number digit by digit.

For i = 0

  • m[i] = '2'
  • Available digits in n: {1, 2, 3}
  • We try to match '2' and it is available, so we use it.
  • At the same time, we check if a smaller digit can be placed.
  • Smaller digit available = 1
  • So we store: bestIndex = 0, bestDigit = 1

For i = 1

  • m[i] = '2'
  • Available digits: {1, 3}
  • We cannot match digit 2 anymore
  • So exact matching with m fails here
  • Now we check for the best possible smaller digit than 2:
  • Available smaller digit = 1
  • So we store: bestIndex = 1, bestDigit = 1

Step 3: Construct final answer using break point

  • We build the result as follows:
  • Take prefix of m up to bestIndex -> "2"
  • Place bestDigit -> "1"
  • So far: res = "21"

Step 4: Add remaining digits

  • Remaining digit = 3
  • We place remaining digits in descending order: res = "213"

Final Answer: "213"


Output
213
Comment