Largest number with one swap allowed
Last Updated :
25 Apr, 2023
Given a positive integer, find the largest number that could be generated by swapping only two digits at most once.
Examples:
Input: 2736
Output : 7236
Explanation:
If we swap the number 2 and the number
7 then the generated number would be
the largest number.
Input : 432
Output : 432
Explanation:
Here, no swap is required. The given
number is already largest.
Approach 1 (Trying every pair):
We convert the number to a string. For each digit of the number, we will swap with positions (i, j) and store it as temp check if the temp is larger than number. If yes then swap back to restore the original number.
C++
#include <bits/stdc++.h>
using namespace std;
int largestNum( int num)
{
string num_in_str = to_string(num);
string temp = num_in_str;
for ( int i = 0; i < num_in_str.size(); i++) {
for ( int j = i + 1; j < num_in_str.size(); j++) {
swap(num_in_str[i], num_in_str[j]);
if (stoi(num_in_str) > stoi(temp))
temp = num_in_str;
swap(num_in_str[i], num_in_str[j]);
}
}
return stoi(temp);
}
int main()
{
int num = 432;
cout << largestNum(num) << endl;
num = 2736;
cout << largestNum(num) << endl;
num = 4596;
cout << largestNum(num) << endl;
return 0;
}
|
Java
public class LargestNumber {
static String swap(String str, int i, int j)
{
char ch[] = str.toCharArray();
char temp = ch[i];
ch[i] = ch[j];
ch[j] = temp;
String c = String.valueOf(ch);
return c;
}
static int largestNum( int num)
{
String num_in_str = "" + num;
String temp = num_in_str;
for ( int i = 0 ; i < num_in_str.length(); i++) {
for ( int j = i + 1 ; j < num_in_str.length();
j++) {
num_in_str = swap(num_in_str, i, j);
if (temp.compareTo(num_in_str) < 0 )
temp = num_in_str;
num_in_str = swap(num_in_str, i, j);
}
}
return Integer.parseInt(temp);
}
public static void main(String[] s)
{
int num = 423 ;
System.out.println(largestNum(num));
num = 2736 ;
System.out.println(largestNum(num));
num = 4596 ;
System.out.println(largestNum(num));
}
}
|
Python3
def largestNum(num):
num_to_str = list ( str (num))
temp = num_to_str[:]
for i in range ( len (num_to_str)):
for j in range (i + 1 , len (num_to_str)):
/ / Swapping current pair
num_to_str[i], num_to_str[j] = num_to_str[j], num_to_str[i]
if num_to_str > temp:
temp = num_to_str[:]
num_to_str[i], num_to_str[j] = num_to_str[j], num_to_str[i]
return int ("".join(temp))
def main():
A = int ( 432 )
print (largestNum(A))
A = int ( 2736 )
print (largestNum(A))
A = int ( 4596 )
print (largestNum(A))
if __name__ = = "__main__" :
main()
|
C#
using System;
public class LargestNumber {
static String swap(String str, int i, int j)
{
char [] ch = str.ToCharArray();
char temp = ch[i];
ch[i] = ch[j];
ch[j] = temp;
String c = String.Join( "" , ch);
return c;
}
static int largestNum( int num)
{
String num_in_str = "" + num;
String temp = num_in_str;
for ( int i = 0; i < num_in_str.Length; i++) {
for ( int j = i + 1; j < num_in_str.Length;
j++) {
num_in_str = swap(num_in_str, i, j);
if (temp.CompareTo(num_in_str) < 0)
temp = num_in_str;
num_in_str = swap(num_in_str, i, j);
}
}
return Convert.ToInt32(temp);
}
public static void Main(String[] s)
{
int num = 423;
Console.WriteLine(largestNum(num));
num = 2736;
Console.WriteLine(largestNum(num));
num = 4596;
Console.WriteLine(largestNum(num));
}
}
|
Javascript
<script>
function largestNum(num)
{
var num_in_str = (num.toString().split( '' ));
var temp = JSON.parse(JSON.stringify(num_in_str));
for ( var i = 0; i < num_in_str.length; i++) {
for ( var j = i + 1; j < num_in_str.length; j++)
{
[num_in_str[i], num_in_str[j]] =
[num_in_str[j], num_in_str[i]];
if (parseInt(num_in_str.join( '' )) >
parseInt(temp.join( '' )))
temp =
JSON.parse(JSON.stringify(num_in_str));
[num_in_str[i], num_in_str[j]] =
[num_in_str[j], num_in_str[i]];
}
}
return parseInt(temp.join( '' ));
}
var num = 432;
document.write( largestNum(num) + "<br>" );
num = 2736;
document.write( largestNum(num) + "<br>" );
num = 4596;
document.write( largestNum(num) + "<br>" );
</script>
|
Output:
432
7236
9546
Time complexity :- O(N^2)
Space complexity :- O(1)
Approach 2 (Efficient) :
We will scan the number from backward direction. In the scan, if the ith digit is the largest by far, store it and its index or if the current digit is smaller than the largest digit recorded by far, this digit and the largest digit are the best suitable for swap.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int largestNum( int num)
{
int max_digit = -1;
int max_digit_indx = -1;
int l_indx = -1;
int r_indx = -1;
string num_in_str = to_string(num);
for ( int i = num_in_str.size() - 1; i >= 0; i--) {
if (num_in_str[i] > max_digit) {
max_digit = num_in_str[i];
max_digit_indx = i;
continue ;
}
if (num_in_str[i] < max_digit) {
l_indx = i;
r_indx = max_digit_indx;
}
}
if (l_indx == -1)
return num;
swap(num_in_str[l_indx], num_in_str[r_indx]);
return stoi(num_in_str);
}
int main()
{
int num = 789;
cout << largestNum(num) << endl;
num = 49658;
cout << largestNum(num) << endl;
num = 2135;
cout << largestNum(num) << endl;
return 0;
}
|
Java
class GFG {
static int largestNum( int num)
{
int max_digit = - 1 ;
int max_digit_indx = - 1 ;
int l_indx = - 1 ;
int r_indx = 1 ;
String num_in_str = String.valueOf(num);
for ( int i = num_in_str.length() - 1 ; i >= 0 ; i--) {
if (num_in_str.charAt(i) > max_digit) {
max_digit = num_in_str.charAt(i);
max_digit_indx = i;
continue ;
}
if (num_in_str.charAt(i) < max_digit) {
l_indx = i;
r_indx = max_digit_indx;
}
}
if (l_indx == - 1 )
return num;
num_in_str = swap(num_in_str, l_indx, r_indx);
return Integer.parseInt(num_in_str);
}
static String swap(String arr, int i, int j)
{
char temp[] = arr.toCharArray();
char c = temp[i];
temp[i] = temp[j];
temp[j] = c;
return String.valueOf(temp);
}
public static void main(String[] args)
{
int num = 789 ;
System.out.println(largestNum(num));
num = 49658 ;
System.out.println(largestNum(num));
num = 2135 ;
System.out.println(largestNum(num));
}
}
|
Python3
def largestNum(num):
max_digit = - 1
max_digit_indx = - 1
l_indx = - 1
r_indx = 1
num_in_str = list ( str (num))
for i in range ( len (num_in_str) - 1 , - 1 , - 1 ):
if int (num_in_str[i]) > int (max_digit):
max_digit = num_in_str[i]
max_digit_indx = i
continue
if int (num_in_str[i]) < int (max_digit):
l_indx = i
r_indx = max_digit_indx
if l_indx = = - 1 :
return num
num_in_str[l_indx], num_in_str[r_indx] = \
num_in_str[r_indx], num_in_str[l_indx]
return int (''.join(num_in_str))
num = 789
print (largestNum(num))
num = 49658
print (largestNum(num))
num = 2135
print (largestNum(num))
|
C#
using System;
class GFG {
static int largestNum( int num)
{
int max_digit = -1;
int max_digit_indx = -1;
int l_indx = -1;
int r_indx = 1;
String num_in_str = String.Join( "" , num);
for ( int i = num_in_str.Length - 1; i >= 0; i--) {
if (num_in_str[r_indx] > max_digit) {
max_digit = num_in_str[i];
max_digit_indx = i;
continue ;
}
if (num_in_str[i] < max_digit) {
l_indx = i;
r_indx = max_digit_indx;
}
}
if (l_indx == -1)
return num;
num_in_str = swap(num_in_str, l_indx, r_indx);
return int .Parse(num_in_str);
}
static String swap(String arr, int i, int j)
{
char [] temp = arr.ToCharArray();
char c = temp[i];
temp[i] = temp[j];
temp[j] = c;
return String.Join( "" , temp);
}
public static void Main(String[] args)
{
int num = 789;
Console.WriteLine(largestNum(num));
num = 49658;
Console.WriteLine(largestNum(num));
num = 2135;
Console.WriteLine(largestNum(num));
}
}
|
Javascript
function largestNum(num) {
let max_digit = -1;
let max_digit_indx = -1;
let l_indx = -1;
let r_indx = 1;
let num_in_str = num.toString().split( '' );
for (let i = num_in_str.length - 1; i >= 0; i--) {
if (parseInt(num_in_str[i]) > parseInt(max_digit)) {
max_digit = num_in_str[i];
max_digit_indx = i;
continue ;
}
if (parseInt(num_in_str[i]) < parseInt(max_digit)) {
l_indx = i;
r_indx = max_digit_indx;
}
}
if (l_indx == -1) {
return num;
}
[num_in_str[l_indx], num_in_str[r_indx]] = [num_in_str[r_indx], num_in_str[l_indx]];
return parseInt(num_in_str.join( '' ));
}
let num = 789;
console.log(largestNum(num));
num = 49658;
console.log(largestNum(num));
num = 2135;
console.log(largestNum(num));
|
Output:
987
94658
5132
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...