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 : 2356
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 ith digit greater than (i+1)th digit, then swap them and print the number else keep on finding the pair. If not found then print “Not Possible” at last.
Below is the implementation of above approach :
- 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 Even number with N digits
- Smallest odd number with N digits
- Smallest odd digits number not less than N
- Smallest even digits number not less than N
- Smallest number with sum of digits as N and divisible by 10^N
- Smallest number with at least n digits in factorial
- Smallest number k such that the product of digits of k is equal to n
- Smallest multiple of a given number made of digits 0 and 9 only
- Find the Largest number with given number of digits and sum of 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 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.