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
- 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
- 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 digits number not less than N
- Smallest odd number with N digits
- Smallest even digits number not less than N
- Smallest Even number with N digits
- Find the kth smallest number with sum of digits as m
- Smallest number with at least n digits in factorial
- Smallest number with sum of digits as N and divisible by 10^N
- Smallest multiple of a given number made of digits 0 and 9 only
- Smallest number k such that the product of digits of k is equal to 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.