# Maximize a number considering permutations with values smaller than limit

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 ** 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 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:

`// 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; ` ` ` ` ` `// Swaping 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) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

213

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Maximize number of groups formed with size not smaller than its largest element
- Maximize count of corresponding same elements in given permutations using cyclic rotations
- Maximize occurrences of values between L and R on sequential addition of Array elements with modulo H
- Permutation of an array that has smaller values from another array
- Find the largest number smaller than integer N with maximum number of set bits
- Previous perfect square and cube number smaller than number N
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Number of palindromic permutations | Set 1
- Count of possible permutations of a number represented as a sum of 2's, 4's and 6's only
- Generate all cyclic permutations of a number
- Find the number of good permutations
- Print all permutations of a number N greater than itself
- Count the number of special permutations
- Find the closest and smaller tidy number
- Largest smaller number possible using only one swap operation
- Largest number smaller than or equal to N divisible by K
- Find largest number smaller than N with same set of digits
- Smallest number containing all possible N length permutations using digits 0 to D
- Number of Permutations such that no Three Terms forms Increasing Subsequence
- Number of unique permutations starting with 1 of a Binary String

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.