Given a number N in the form of string. The task is to find the greatest number that has same set of digits as N and is smaller than N. If it is not possible to find any such numebr, then print “not possible”.
Input: N = "218765" Output: 218756 Input: N = "1234" Output: Not Possible Input: N = "262345" Output: 256432
Following are few observations about the greatest number smaller than N:
- If all digits sorted in ascending order, then output is always “Not Possible”. For example, 1234.
- If all digits are sorted in descending order, then we need to swap last two digits. For example, 4321.
- For other cases, we need to process the number from rightmost side (why? because we need to find the greatest of all smaller numbers).
- Traverse the given number from rightmost digit, keep traversing till you find a digit which is greater than the previously traversed digit. For example, if the input number is “262345”, we stop at 6 because 6 is greater than previous digit 5. If we do not find such a digit, then output is “Not Possible”.
- Now search the right side of above found digit ‘d’ for the greatest digit smaller than ‘d’. For “262345?, the right side of 6 contains “2345”. The greatest digit smaller than 6 is 5.
- Swap the above found two digits, we get “252346” in above example.
- Now sort all digits from position next to ‘d’ to the end of number in descending order. The number that we get after sorting is the output. For above example, we sort digits in bold 252346. We get 256432 which is the previous smaller number for input 262345.</li
Below is the implementation of the above approach:
Greatest smaller number with same set of digits is 256432
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find the largest number smaller than integer N with maximum number of set bits
- Largest number smaller than or equal to n and digits in non-decreasing order
- Largest number smaller than or equal to N divisible by K
- Maximize number of groups formed with size not smaller than its largest element
- Minimum digits to be removed to make either all digits or alternating digits same
- Find the Largest number with given number of digits and sum of digits
- Largest element smaller than current element on left for every element in Array
- Previous perfect square and cube number smaller than number N
- Minimum number of digits to be removed so that no two consecutive digits are same
- Next greater Number than N with the same quantity of digits A and B
- Largest number not greater than N all the digits of which are odd
- Largest number not greater than N which can become prime after rearranging its digits
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Largest number less than N with digit sum greater than the digit sum of N
- Largest smaller number possible using only one swap operation
- Find next greater number with same set of digits
- Maximize a number considering permutations with values smaller than limit
- Find smallest possible Number from a given large Number with same count of digits
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.