# Greatest number less than equal to B that can be formed from the digits of A

Given two integers **A** and **B**, the task is to find the greatest number **≤ B** that can be formed using all the digits of **A**.

**Examples:**

Input:A = 123, B = 222

Output:213

123, 132 and 213 are the only valid numbers which are ≤ 222.

213 is the maximum among them.

Input:A = 3921, B = 10000

Output:9321

**Approach:** Let’s construct the answer digit by digit starting from the leftmost. We need to build a lexicographically maximal answer so we should choose the greatest digit in each step.

Iterate over all possible digits starting from the greatest. For each digit check if it’s possible to put it in this position. For this, construct minimal suffix (greedily put the lowest digit) and compare the resulting number with B. If it is less than or equal to B then proceed to the next digit.

Below is the implementation of the above approach:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the greatest number ` `// not gtreater than B that can be formed ` `// with the digits of A ` `string Permute_Digits(string a, ` `long` `long` `b) ` `{ ` ` ` `// To store size of A ` ` ` `int` `n = a.size(); ` ` ` ` ` `// To store the required answer ` ` ` `string ans = ` `""` `; ` ` ` ` ` `// Traverse from leftmost digit and ` ` ` `// place a smaller digit for every ` ` ` `// position. ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// Keep all digits in A ` ` ` `set<` `char` `> temp(a.begin(), a.end()); ` ` ` ` ` `// To avoid leading zeros ` ` ` `if` `(i == 0) ` ` ` `temp.erase(0); ` ` ` ` ` `// For all possible values at ith position from ` ` ` `// largest value to smallest ` ` ` `for` `(` `auto` `j = temp.rbegin(); j != temp.rend(); ++j) { ` ` ` ` ` `// Take largest possible digit ` ` ` `string s1 = ans + *j; ` ` ` ` ` `// Keep duplicate of string a ` ` ` `string s2 = a; ` ` ` ` ` `// Remove the taken digit from s2 ` ` ` `s2.erase(s2.find(*j), 1); ` ` ` ` ` `// Sort all the remaining digits of s2 ` ` ` `sort(s2.begin(), s2.end()); ` ` ` ` ` `// Add s2 to current s1 ` ` ` `s1 += s2; ` ` ` ` ` `// If s1 is less than B then it can be ` ` ` `// included in the answer. Note that ` ` ` `// stoll() converts a string to lomg ` ` ` `// long int. ` ` ` `if` `(stoll(s1) <= b) { ` ` ` `ans += *j; ` ` ` ` ` `// change A to s2 ` ` ` `a = s2; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return the required answer ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string a = ` `"123"` `; ` ` ` `int` `b = 222; ` ` ` `cout << Permute_Digits(a, b); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

213

**Optimizations** : We can use multiset to keep all occurrences of a digit in set. We can also do binary search using lower_bound() in C++ to quickly find the digit to be placed.

## Recommended Posts:

- Find maximum number that can be formed using digits of a given number
- Find the largest number that can be formed by changing at most K digits
- Check if the number formed by the last digits of N numbers is divisible by 10 or not
- Recursive sum of digits of a number formed by repeated appends
- Find all strings formed from characters mapped to digits of a number
- Find the Largest Cube formed by Deleting minimum Digits from a number
- Sum of digits equal to a given number in PL/SQL
- Find a Number X whose sum with its digits is equal to N
- Find a number x such that sum of x and its digits is equal to given n.
- Smallest number k such that the product of digits of k is equal to n
- Check if product of digits of a number at even and odd places is equal
- Largest number smaller than or equal to n and digits in non-decreasing order
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Smallest multiple of N formed using the given set of digits
- Maximum possible time that can be formed from four digits

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.