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++
#include <bits/stdc++.h>
using namespace std;
string Permute_Digits(string a, long long b)
{
int n = a.size();
string ans = "" ;
for ( int i = 0; i < n; i++) {
set< char > temp(a.begin(), a.end());
if (i == 0)
temp.erase(0);
for ( auto j = temp.rbegin(); j != temp.rend(); ++j) {
string s1 = ans + *j;
string s2 = a;
s2.erase(s2.find(*j), 1);
sort(s2.begin(), s2.end());
s1 += s2;
if (stoll(s1) <= b) {
ans += *j;
a = s2;
break ;
}
}
}
return ans;
}
int main()
{
string a = "123" ;
int b = 222;
cout << Permute_Digits(a, b);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static String Permute_Digits(String a, int b)
{
int n = a.length();
String ans = "" ;
for ( int i = 0 ; i < n; i++) {
HashSet<Character> temp = new HashSet<Character>();
for ( int x= 0 ;x<a.length();x++){
if (a.charAt(x) != '0' ) temp.add(a.charAt(x));
}
char [] reverse = new char [temp.size()];
int pos = temp.size() - 1 ;
for ( char x : temp)
{
reverse[pos--] = x;
}
for ( int j = 0 ; j < reverse.length; j++) {
String s1 = ans + reverse[j];
String s2 = "" + a;
int ind = s2.indexOf(reverse[j]);
if (ind != - 1 )
s2 = s2.substring( 0 , ind) + s2.substring(ind + 1 );
char [] s2arr = s2.toCharArray();
Arrays.sort(s2arr);
s2 = new String(s2arr);
s1 += s2;
if (Integer.valueOf(s1) <= b) {
ans += reverse[j];
a = s2;
break ;
}
}
}
return ans;
}
public static void main(String[] args)
{
String a = "123" ;
int b = 222 ;
System.out.println(Permute_Digits(a, b));
}
}
|
Python3
def permuteDigits(a: str , b: int ) - > str :
n = len (a)
ans = ""
for i in range (n):
temp = set ( list (a))
if i = = 0 :
temp.discard( 0 )
for j in reversed ( list (temp)):
s1 = ans + j
s2 = list (a).copy()
s2.remove(j)
s2 = ''.join( sorted ( list (s2)))
s1 + = s2
if int (s1) < = b:
ans + = j
a = ''.join( list (s2))
break
return ans
if __name__ = = "__main__" :
a = "123"
b = 222
print (permuteDigits(a, b))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static string Permute_Digits( string a, int b)
{
int n = a.Length;
string ans = "" ;
for ( int i = 0; i < n; i++) {
HashSet< char > temp = new HashSet< char >();
for ( int x=0;x<a.Length;x++){
if (a[x] != '0' ) temp.Add(a[x]);
}
char [] reverse = new char [temp.Count];
temp.CopyTo(reverse);
Array.Reverse(reverse);
for ( int j = 0; j < reverse.Length; j++) {
string s1 = ans + reverse[j];
string s2 = "" + a;
if (s2.IndexOf(reverse[j]) != -1)
s2 = s2.Remove(s2.IndexOf(reverse[j]), 1);
char [] s2arr = s2.ToCharArray();
Array.Sort(s2arr);
s2 = new string (s2arr);
s1 += s2;
if (Convert.ToInt32(s1) <= b) {
ans += reverse[j];
a = s2;
break ;
}
}
}
return ans;
}
public static void Main( string [] args)
{
string a = "123" ;
int b = 222;
Console.Write(Permute_Digits(a, b));
}
}
|
Javascript
<script>
function Permute_Digits(a,b)
{
let n = a.length;
let ans = "" ;
for (let i = 0; i < n; i++) {
let temp = new Set()
for (let i=0;i<n;i++){
if (a[i] !== '0' )temp.add(a[i])
}
let reverse = [...temp].reverse();
for (let j = 0; j < reverse.length; j++) {
let s1 = ans + reverse[j];
let s2 = a;
s2 = s2.replace(reverse[j], '' );
s2 = [...s2].sort((a,b)=>a-b).join( "" );
s1 += s2;
if (Number(s1) <= b) {
ans += reverse[j];
a = s2;
break ;
}
}
}
return ans;
}
let a = "123" ;
let b = 222;
document.write(Permute_Digits(a, b));
</script>
|
Time Complexity: O(|a|2)
Auxiliary Space: O(|a|)
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.
Last Updated :
24 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...