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.
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.
# Python3 program to find the largest smaller
# number by swapping one digit.
# Returns largest possible number
# with one swap such that the number
# is smaller than str. It is assumed
# that there are leading 0s.
def prevNum(string, n):
index = -1
# Traverse from right until we find
# a digit which is greater than its
# next digit. For example, in 34125,
# our index is 4.
for i in range(n – 2, -1, -1):
if int(string[i]) > int(string[i + 1]):
index = i
# We can also use binary search here as
# digits after index are sorted in
# increasing order.
# Find the biggest digit in the right of
# arr[index] which is smaller than arr[index]
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[i]) < int(string[index])): smallGreatDgt = i # If index is -1 i.e. digits are # in increasing order. if index == -1: return "" . join("-1") else: # Swap both values (string[index], string[smallGreatDgt]) = (string[smallGreatDgt], string[index]) return "" . join(string) # Driver Code if __name__=='__main__': n_str = "34125" ans = prevNum(list(n_str), len(n_str)) print(ans) # This code is contributed by Vikash Kumar 37 [tabby title="PHP"]
- Form the largest number using at most one swap operation
- Largest even number possible by using one swap operation in given number
- Form the smallest number using at most one swap operation
- Next higher number using atmost one swap operation
- Find largest number smaller than N with same set of digits
- Largest number smaller than or equal to N divisible by K
- Largest number with one swap allowed
- Largest number smaller than or equal to n and digits in non-decreasing order
- Minimum number of given operation required to convert n to m
- Generate number with given operation and check if it is palindrome
- Find the closest and smaller tidy number
- Maximize a number considering permutations with values smaller than limit
- Check if the given array can be reduced to zeros with the given operation performed given number of times
- Number of times the largest perfect square number can be subtracted from N
- Find the largest good number in the divisors of given number N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.