Form the smallest number using at most one swap operation
Given a non-negative number num. The problem is to apply at most one swap operation on the number num so that the resultant is the smallest possible number. The number could be very large so a string type can be used to store the number. The input does not contain leading 0’s and the output should also not contain leading 0’s.
Note: The same set of digits should be there in the resultant number as was there in the original number.
Examples:
Input : n = 9625635
Output : 2695635
Swapped the digits 9 and 2.
Input : n = 1205763
Output : 1025763
Approach:
- Create an array rightMin[].
- rightMin[i] contains the index of the smallest digit which is on the right side of num[i] and also smaller than num[i].
- If no such digit exists then rightMin[i] = -1. Now, check that whether num[0] has a right smaller digit which is not equal to 0.
- If so then swap 1st digit with its right smaller digit. Else, traverse the rightMin[] array from i = 1 to n-1(where n is the total number of digits in num), and find the first element having rightMin[i] != -1.
- Perform the swap(num[i], num[rightMin[i]]) operation and break.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
string smallestNumber(string num)
{
int n = num.size();
int rightMin[n], right;
rightMin[n - 1] = -1;
right = n - 1;
for ( int i = n - 2; i >= 1; i--) {
if (num[i] >= num[right])
rightMin[i] = right;
else {
if (num[i] == num[i + 1]) {
rightMin[i] = right;
}
else {
rightMin[i] = -1;
right = i;
}
}
}
int small = -1;
for ( int i = 1; i < n; i++)
if (num[i] != '0' ) {
if (small == -1) {
if (num[i] < num[0])
small = i;
}
else if (num[i] <= num[small])
small = i;
}
if (small != -1)
swap(num[0], num[small]);
else {
for ( int i = 1; i < n; i++) {
if (rightMin[i] != -1 && num[i] != num[rightMin[i]]) {
swap(num[i], num[rightMin[i]]);
break ;
}
}
}
return num;
}
int main()
{
string num = "9625635" ;
cout << "Smallest number: "
<< smallestNumber(num);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
public class GeeksforGeeks {
public static String smallestNumber(String str)
{
char [] num = str.toCharArray();
int n = str.length();
int [] rightMin = new int [n];
rightMin[n - 1 ] = - 1 ;
int right = n - 1 ;
for ( int i = n - 2 ; i >= 1 ; i--) {
if (num[i] > num[right])
rightMin[i] = right;
else {
rightMin[i] = - 1 ;
right = i;
}
}
int small = - 1 ;
for ( int i = 1 ; i < n; i++)
if (num[i] != '0' ) {
if (small == - 1 ) {
if (num[i] < num[ 0 ])
small = i;
}
else if (num[i] < num[small])
small = i;
}
if (small != - 1 ) {
char temp;
temp = num[ 0 ];
num[ 0 ] = num[small];
num[small] = temp;
}
else {
for ( int i = 1 ; i < n; i++) {
if (rightMin[i] != - 1 ) {
char temp;
temp = num[i];
num[i] = num[rightMin[i]];
num[rightMin[i]] = temp;
break ;
}
}
}
return ( new String(num));
}
public static void main(String argc[])
{
String num = "9625635" ;
System.out.println( "Smallest number: " + smallestNumber(num));
}
}
|
Python 3
def smallestNumber(num):
num = list (num)
n = len (num)
rightMin = [ 0 ] * n
right = 0
rightMin[n - 1 ] = - 1 ;
right = n - 1 ;
for i in range (n - 2 , 0 , - 1 ):
if num[i] > num[right]:
rightMin[i] = right
else :
rightMin[i] = - 1
right = i
small = - 1
for i in range ( 1 , n):
if num[i] ! = '0' :
if small = = - 1 :
if num[i] < num[ 0 ]:
small = i
elif num[i] < num[small]:
small = i
if small ! = - 1 :
num[ 0 ], num[small] = num[small], num[ 0 ]
else :
for i in range ( 1 , n):
if rightMin[i] ! = - 1 :
num[i], num[rightMin[i]] = num[rightMin[i]], num[i]
break
return ''.join(num)
if __name__ = = "__main__" :
num = "9625635"
print ( "Smallest number: " , smallestNumber(num))
|
C#
using System;
public class GeeksforGeeks {
public static String smallestNumber(String str)
{
char [] num = str.ToCharArray();
int n = str.Length;
int [] rightMin = new int [n];
rightMin[n - 1] = -1;
int right = n - 1;
for ( int i = n - 2; i >= 1; i--) {
if (num[i] > num[right])
rightMin[i] = right;
else {
rightMin[i] = -1;
right = i;
}
}
int small = -1;
for ( int i = 1; i < n; i++)
if (num[i] != '0' ) {
if (small == -1) {
if (num[i] < num[0])
small = i;
}
else if (num[i] < num[small])
small = i;
}
if (small != -1) {
char temp;
temp = num[0];
num[0] = num[small];
num[small] = temp;
}
else {
for ( int i = 1; i < n; i++) {
if (rightMin[i] != -1) {
char temp;
temp = num[i];
num[i] = num[rightMin[i]];
num[rightMin[i]] = temp;
break ;
}
}
}
return ( new String(num));
}
public static void Main()
{
String num = "9625635" ;
Console.Write( "Smallest number: " + smallestNumber(num));
}
}
|
PHP
Javascript
Output
Smallest number: 2695635
Time Complexity: O(n), where n is the total number of digits.
Auxiliary Space: O(n), where n is the total number of digits.
Last Updated :
18 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...