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 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 smallest number with given number of digits and sum of digits
- Find the smallest number whose digits multiply to a given number n
- Smallest number with given sum of digits and sum of square of digits
- 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
- Smallest even digits number not less than N
- Smallest Even number with N digits
- Smallest odd number with N digits
- Smallest odd digits number not less than N
- Smallest number with sum of digits as N and divisible by 10^N
- Find the smallest number whose sum of digits is N
- Smallest number whose sum of digits is square of N
- Find the kth smallest number with sum of digits as m
- Smallest number whose square has N digits
- Smallest number with at least n digits in factorial
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.