Given a number n, find the smallest number that has same set of digits as n and is greater than n. If x is the greatest possible number with its set of digits, then print “not possible”.

Examples:
For simplicity of implementation, we have considered input number as a string.

Input: n = “218765”
Output: “251678”

Input: n = “1234”
Output: “1243”

Input: n = “4321”
Output: “Not Possible”

Input: n = “534976”
Output: “536479”

Following are few observations about the next greater number.
1) If all digits sorted in descending order, then output is always “Not Possible”. For example, 4321.
2) If all digits are sorted in ascending order, then we need to swap last two digits. For example, 1234.
3) For other cases, we need to process the number from rightmost side (why? because we need to find the smallest of all greater numbers)

[ad type=”banner”]

You can now try developing an algorithm yourself.

Following is the algorithm for finding the next greater number.
I) Traverse the given number from rightmost digit, keep traversing till you find a digit which is smaller than the previously traversed digit. For example, if the input number is “534976”, we stop at 4 because 4 is smaller than next digit 9. If we do not find such a digit, then output is “Not Possible”.

II) Now search the right side of above found digit ‘d’ for the smallest digit greater than ‘d’. For “534976″, the right side of 4 contains “976”. The smallest digit greater than 4 is 6.

III) Swap the above found two digits, we get 536974 in above example.

IV) Now sort all digits from position next to ‘d’ to the end of number. The number that we get after sorting is the output. For above example, we sort digits in bold 536974. We get “536479” which is the next greater number for input 534976.

[ad type=”banner”]

Following is C++ implementation of above approach.

[pastacode lang=”cpp” manual=”%2F%2F%20C%2B%2B%20program%20to%20find%20the%20smallest%20number%20which%20greater%20than%20a%20given%20number%0A%2F%2F%20and%20has%20same%20set%20of%20digits%20as%20given%20number%0A%23include%20%3Ciostream%3E%0A%23include%20%3Ccstring%3E%0A%23include%20%3Calgorithm%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20Utility%20function%20to%20swap%20two%20digits%0Avoid%20swap(char%20*a%2C%20char%20*b)%0A%7B%0A%20%20%20%20char%20temp%20%3D%20*a%3B%0A%20%20%20%20*a%20%3D%20*b%3B%0A%20%20%20%20*b%20%3D%20temp%3B%0A%7D%0A%20%0A%2F%2F%20Given%20a%20number%20as%20a%20char%20array%20number%5B%5D%2C%20this%20function%20finds%20the%0A%2F%2F%20next%20greater%20number.%20%20It%20modifies%20the%20same%20array%20to%20store%20the%20result%0Avoid%20findNext(char%20number%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20int%20i%2C%20j%3B%0A%20%0A%20%20%20%20%2F%2F%20I)%20Start%20from%20the%20right%20most%20digit%20and%20find%20the%20first%20digit%20that%20is%0A%20%20%20%20%2F%2F%20smaller%20than%20the%20digit%20next%20to%20it.%0A%20%20%20%20for%20(i%20%3D%20n-1%3B%20i%20%3E%200%3B%20i–)%0A%20%20%20%20%20%20%20%20if%20(number%5Bi%5D%20%3E%20number%5Bi-1%5D)%0A%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%20%0A%20%20%20%20%2F%2F%20If%20no%20such%20digit%20is%20found%2C%20then%20all%20digits%20are%20in%20descending%20order%0A%20%20%20%20%2F%2F%20means%20there%20cannot%20be%20a%20greater%20number%20with%20same%20set%20of%20digits%0A%20%20%20%20if%20(i%3D%3D0)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22Next%20number%20is%20not%20possible%22%3B%0A%20%20%20%20%20%20%20%20return%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20II)%20Find%20the%20smallest%20digit%20on%20right%20side%20of%20(i-1)’th%20digit%20that%20is%0A%20%20%20%20%2F%2F%20greater%20than%20number%5Bi-1%5D%0A%20%20%20%20int%20x%20%3D%20number%5Bi-1%5D%2C%20smallest%20%3D%20i%3B%0A%20%20%20%20for%20(j%20%3D%20i%2B1%3B%20j%20%3C%20n%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20if%20(number%5Bj%5D%20%3E%20x%20%26%26%20number%5Bj%5D%20%3C%20number%5Bsmallest%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20smallest%20%3D%20j%3B%0A%20%0A%20%20%20%20%2F%2F%20III)%20Swap%20the%20above%20found%20smallest%20digit%20with%20number%5Bi-1%5D%0A%20%20%20%20swap(%26number%5Bsmallest%5D%2C%20%26number%5Bi-1%5D)%3B%0A%20%0A%20%20%20%20%2F%2F%20IV)%20Sort%20the%20digits%20after%20(i-1)%20in%20ascending%20order%0A%20%20%20%20sort(number%20%2B%20i%2C%20number%20%2B%20n)%3B%0A%20%0A%20%20%20%20cout%20%3C%3C%20%22Next%20number%20with%20same%20set%20of%20digits%20is%20%22%20%3C%3C%20number%3B%0A%20%0A%20%20%20%20return%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20char%20digits%5B%5D%20%3D%20%22534976%22%3B%0A%20%20%20%20int%20n%20%3D%20strlen(digits)%3B%0A%20%20%20%20findNext(digits%2C%20n)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C++ Progrzm” highlight=”” provider=”manual”/]

Output:

Next number with same set of digits is 536479
The above implementation can be optimized in following ways.
1) We can use binary search in step II instead of linear search.
2) In step IV, instead of doing simple sort, we can apply some clever technique to do it in linear time. Hint: We know that all digits are linearly sorted in reverse order except one digit which was swapped.

With above optimizations, we can say that the time complexity of this method is O(n).

[ad type=”banner”]