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 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
#include <bits/stdc++.h>
using namespace std;
string maximizeNumber(string N, int M)
{
sort(N.begin(), N.end());
for ( int i = 0; i < N.size(); i++) {
for ( int j = i + 1; j < N.size(); j++) {
string t = N;
swap(t[j], t[i]);
sort(t.begin() + i + 1, t.end());
if (stoll(t) > stoll(N) && stoll(t) <= M)
swap(N[i], N[j]);
}
}
return N;
}
int main()
{
string N = "123" ;
int M = 222;
cout << maximizeNumber(N, M);
return 0;
}
|
Java
import java.io.*;
import java.util.Arrays;
class GFG {
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);
}
public static String
sortString(String string, int s_index, int e_index)
{
char tempArray[] = string.toCharArray();
Arrays.sort(tempArray, s_index, e_index);
return new String(tempArray);
}
public static String maximizeNumber(String N, int M)
{
N = sortString(N, 0 , N.length());
for ( int i = 0 ; i < N.length(); i++) {
for ( int j = i + 1 ; j < N.length(); j++) {
String t = N;
t = swap(t, j, i);
t = sortString(t, i + 1 , t.length());
if (Long.parseLong(t) > Long.parseLong(N)
&& Long.parseLong(t) <= M)
N = swap(N, i, j);
}
}
return N;
}
public static void main(String[] args)
{
String N = "123" ;
int M = 222 ;
System.out.println(maximizeNumber(N, M));
}
}
|
Python3
def swap( str , i, j):
ch = list ( str )
temp = ch[i]
ch[i] = ch[j]
ch[j] = temp
return "".join(ch)
def sortString( str , s_index, e_index):
tempArray = list ( str )
tempArray = tempArray[:s_index] + sorted (tempArray[s_index: e_index])
return "".join(tempArray)
def maximizeNumber(N, M):
N = sortString(N, 0 , len (N))
for i in range ( len (N)):
for j in range (i + 1 , len (N)):
t = N
t = swap(t, j, i)
t = sortString(t, i + 1 , len (t))
if ( int (t) > int (N) and int (t) < = M):
N = swap(N, i, j)
return N
N = "123"
M = 222
print (maximizeNumber(N, M))
|
C#
using System;
using System.Collections.Generic;
class GFG {
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);
}
public static string sortString( string str, int s_index,
int e_index)
{
char [] tempArray = str.ToCharArray();
Array.Sort(tempArray, s_index, e_index - s_index);
return new string (tempArray);
}
public static string maximizeNumber( string N, int M)
{
N = sortString(N, 0, N.Length);
for ( int i = 0; i < N.Length; i++) {
for ( int j = i + 1; j < N.Length; j++) {
string t = N;
t = swap(t, j, i);
t = sortString(t, i + 1, t.Length);
if (Convert.ToInt64(t) > Convert.ToInt64(N)
&& Convert.ToInt64(t) <= M)
N = swap(N, i, j);
}
}
return N;
}
public static void Main( string [] args)
{
string N = "123" ;
int M = 222;
Console.WriteLine(maximizeNumber(N, M));
}
}
|
Javascript
function swap(str, i, j)
{
let ch = str.split( "" );
let temp = ch[i];
ch[i] = ch[j];
ch[j] = temp;
return ch.join( "" );
}
function sortString(str, s_index, e_index)
{
let tempArray = str.split( "" );
tempArray
= tempArray.slice(0, s_index)
.concat(
tempArray.slice(s_index, e_index).sort());
return tempArray.join( "" );
}
function maximizeNumber(N, M)
{
N = sortString(N, 0, N.length);
for ( var i = 0; i < N.length; i++) {
for ( var j = i + 1; j < N.length; j++) {
let t = N;
t = swap(t, j, i);
t = sortString(t, i + 1, t.length);
if (parseInt(t) > parseInt(N)
&& parseInt(t) <= M)
N = swap(N, i, j);
}
}
return N;
}
let N = "123" ;
let M = 222;
console.log(maximizeNumber(N, M));
|
Time complexity : O(n3 log n)
Auxiliary Space: O(n), since n extra space has been taken.
Last Updated :
02 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...