Given a number print the immediate smallest number formed by re-arranging the digits of a given number.
Print “Not Possible” if it is not possible to get the smallest number.
Input : n = 1234
Output : Not Possible
Input : n = 3544
Output : 3454
Input : n = 2536
Output : 2365
Source :D-e-Shaw Interview Experience
This problem is a variation of this article. In this article we have to find the immediate smallest number, So idea is to traverse the number from last and if we find (i-1)th digit greater than (i)th digit, then store this index. Then find the greatest digit on the right side of (index-1)’th digit that is smaller than digits[index-1] and swap them. After that sort the digits after (index-1)th digit in descending order.
Below is the implementation of above approach :
Immediate Smaller No. is 2365
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.
- Smallest number by rearranging digits of a given number
- Largest number not greater than N which can become prime after rearranging its digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Find last 2 survivors in N persons standing in a circle after killing next to immediate neighbour
- Find smallest number with given number of digits and sum of digits
- Smallest number with given sum of digits and sum of square of digits
- Largest palindromic string possible from given strings by rearranging the characters
- Cost of rearranging the array such that no element exceeds the sum of its adjacent elements
- Count of distinct XORs formed by rearranging two Binary strings
- Minimum cost to remove the spaces between characters of a String by rearranging the characters
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Minimum digits to be removed to make either all digits or alternating digits same
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Find the smallest number whose digits multiply to a given number n
- Get the kth smallest number using the digits of the given number
- Smallest odd number with even sum of digits from the given number N
- Find smallest possible Number from a given large Number with same count of digits
- Find smallest number formed by inverting digits of given number N
- Find the Largest number with given number of digits and sum of digits
- Number of digits in the nth number made of given four 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.