Largest smaller number possible using only one swap operation
Last Updated :
15 Dec, 2022
Given a non-negative number N in the form of string. The task is to apply at most one swap operation on the number N so that the resultant is smaller than N and is the largest such number.
Examples:
Input :str = "12435"
Output : 12345
Although the number 12354 will be the
largest smaller number from 12435. But
it is not possible to make it using only
one swap. So swap 4 and 3 and get 12345.
Input : 34123567
Output : 33124567
We swap 4 with 3 (on its right side) to
get the largest smaller number.
Input : str = " 12345"
Output : -1
Digits are in increasing order. So it
is not possible to make a smaller number
from it.
Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.
- Start traversing from right, find a digit is which is greater than one of the digits on its right. Let this index such element be index.
- Then find another index on right of str[index] which holds the greatest value smaller than str[index].
- Swap two values found above.
C++
#include <bits/stdc++.h>
using namespace std;
string prevNum(string str)
{
int len = str.length();
int index = -1;
for ( int i = len - 2; i >= 0; i--) {
if (str[i] > str[i+1])
{
index = i;
break ;
}
}
int smallGreatDgt = -1;
for ( int i = len - 1; i > index; i--) {
if (str[i] < str[index]) {
if (smallGreatDgt == -1)
smallGreatDgt = i;
else if (str[i] >= str[smallGreatDgt])
smallGreatDgt = i;
}
}
if (index == -1)
return "-1" ;
if (smallGreatDgt != -1)
{
swap(str[index], str[smallGreatDgt]);
return str;
}
return "-1" ;
}
int main()
{
string str = "34125" ;
cout << prevNum(str);
return 0;
}
|
Java
import java.io.*;
public class GFG
{
static String prevNum(String str)
{
int len = str.length();
int index = - 1 ;
for ( int i = len - 2 ; i >= 0 ; i--)
{
if (str.charAt(i) > str.charAt(i + 1 ))
{
index = i;
break ;
}
}
int smallGreatDgt = - 1 ;
for ( int i = len - 1 ; i > index; i--)
{
if (str.charAt(i) < str.charAt(index))
{
if (smallGreatDgt == - 1 )
{
smallGreatDgt = i;
}
else if (str.charAt(i) >=
str.charAt(smallGreatDgt))
{
smallGreatDgt = i;
}
}
}
if (index == - 1 )
{
return "-1" ;
}
if (smallGreatDgt != - 1 )
{
str = swap(str, index, smallGreatDgt);
return str;
}
return "-1" ;
}
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 String.valueOf(ch);
}
public static void main(String[] args)
{
String str = "34125" ;
System.out.println(prevNum(str));
}
}
|
Python3
import sys
def prevNum(string, n):
index = - 1
for i in range (n - 2 , - 1 , - 1 ):
if int (string[i]) > int (string[i + 1 ]):
index = i
break
smallGreatDgt = - 1
for i in range (n - 1 , index, - 1 ):
if (smallGreatDgt = = - 1 and int (string[i]) <
int (string[index])):
smallGreatDgt = i
elif (index > - 1 and int (string[i]) > =
int (string[smallGreatDgt]) and
int (string[i]) < int (string[index])):
smallGreatDgt = i
if index = = - 1 :
return " " . join(" - 1 ")
else :
(string[index],
string[smallGreatDgt]) = (string[smallGreatDgt],
string[index])
return "" . join(string)
if __name__ = = '__main__' :
n_str = "34125"
ans = prevNum( list (n_str), len (n_str))
print (ans)
|
C#
using System;
class GFG
{
static String prevNum(String str)
{
int len = str.Length;
int index = -1;
for ( int i = len - 2; i >= 0; i--)
{
if (str[i] > str[i + 1])
{
index = i;
break ;
}
}
int smallGreatDgt = -1;
for ( int i = len - 1; i > index; i--)
{
if (str[i] < str[index])
{
if (smallGreatDgt == -1)
{
smallGreatDgt = i;
}
else if (str[i] >=
str[smallGreatDgt])
{
smallGreatDgt = i;
}
}
}
if (index == -1)
{
return "-1" ;
}
if (smallGreatDgt != -1)
{
str = swap(str, index, smallGreatDgt);
return str;
}
return "-1" ;
}
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 String.Join( "" ,ch);
}
public static void Main(String[] args)
{
String str = "34125" ;
Console.WriteLine(prevNum(str));
}
}
|
PHP
<?php
function prevNum( $str )
{
$len = strlen ( $str );
$index = -1;
for ( $i = $len - 2; $i >= 0; $i --)
{
if ( $str [ $i ] > $str [ $i + 1])
{
$index = $i ;
break ;
}
}
$smallGreatDgt = -1;
for ( $i = $len - 1;
$i > $index ; $i --)
{
if ( $str [ $i ] < $str [ $index ])
{
if ( $smallGreatDgt == -1)
$smallGreatDgt = $i ;
else if ( $str [ $i ] >= $str [ $smallGreatDgt ])
$smallGreatDgt = $i ;
}
}
if ( $index == -1)
return "-1" ;
if ( $smallGreatDgt != -1)
{
list( $str [ $index ],
$str [ $smallGreatDgt ]) = array ( $str [ $smallGreatDgt ],
$str [ $index ]);
return $str ;
}
return "-1" ;
}
$str = "34125" ;
echo prevNum( $str );
?>
|
Javascript
<script>
function prevNum(str)
{
let len = str.length;
let index = -1;
for (let i = len - 2; i >= 0; i--)
{
if (str[i] > str[i + 1])
{
index = i;
break ;
}
}
let smallGreatDgt = -1;
for (let i = len - 1; i > index; i--)
{
if (str[i] < str[index])
{
if (smallGreatDgt == -1)
{
smallGreatDgt = i;
}
else if (str[i] >= str[smallGreatDgt])
{
smallGreatDgt = i;
}
}
}
if (index == -1)
{
return "-1" ;
}
if (smallGreatDgt != -1)
{
str = swap(str, index, smallGreatDgt);
return str;
}
return "-1" ;
}
function swap(str, i, j)
{
let ch = str.split( '' );
let temp = ch[i];
ch[i] = ch[j];
ch[j] = temp;
return ch.join( "" );
}
let str = "34125" ;
document.write(prevNum(str));
</script>
|
Output:
32145
Time Complexity: O(N), where N is the size of the given string str
Auxiliary Space: O(1),as no extra space is required
Share your thoughts in the comments
Please Login to comment...