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.
Input : n = 9625635 Output : 2695635 Swapped the digits 9 and 2. Input : n = 1205763 Output : 1025763
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 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.
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.
This article is contributed by Ayush Jauhari. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Form the largest number using at most one swap operation
- Next higher number using atmost one swap operation
- Largest smaller number possible using only one swap operation
- Largest even number possible by using one swap operation in given number
- Arrange given numbers to form the smallest number
- Form smallest number using indices of numbers chosen from Array with sum less than S
- Form lexicographically smallest string with minimum replacements having equal number of 0s, 1s and 2s
- Swap all occurrences of two characters to get lexicographically smallest string
- Lexicographically smallest array formed by at most one swap for every pair of adjacent indices
- Minimum possible number with the given operation
- Maximum possible number with the given operation
- Number of ways to swap two bit of s1 so that bitwise OR of s1 and s2 changes
- Largest number with one swap allowed
- Transform One String to Another using Minimum Number of Given Operation
- Number of ways to color N-K blocks using given operation
- Count the total number of triangles after Nth operation
- Generate number with given operation and check if it is palindrome
- Minimum number of given operation required to convert n to m
- Check if X and Y can be made zero by using given operation any number of times
- Check if the given array can be reduced to zeros with the given operation performed given number of times