# Immediate smallest number after re-arranging the digits of a given number

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.

Examples:

Input : n = 1234
Output : Not Possible

Input : n = 3544
Output : 3454

Input : n = 2536
Output : 2365

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 :

## C++

 `// C++ program for immediate smallest number ` `// after re-arranging the digits of a given number ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Utility function to swap two digits ` `void` `swap(``char``* a, ``char``* b) ` `{ ` `    ``char` `temp = *a; ` `    ``*a = *b; ` `    ``*b = temp; ` `} ` ` `  `void` `immediateSmallest(``char` `digits[], ``int` `l) ` `{ ` ` `  `    ``// Find the last digit which is smaller ` `    ``// than previous digit ` `    ``int` `index = -1; ` `    ``for` `(``int` `i = l - 1; i > 0; i--) { ` `        ``if` `(digits[i] < digits[i - 1]) { ` `            ``index = i; ` `            ``break``; ` `        ``} ` `    ``} ` `    ``// If no such digit is found, then all digits are in ascending order ` `    ``// means there cannot be a smaller number with same set of digits ` `    ``if` `(index == -1) { ` `        ``cout << ``"Not Possible\n"``; ` `        ``return``; ` `    ``} ` `    ``// Find the greatest digit on right side of (index-1)'th digit that is ` `    ``// smaller than digits[index-1] ` `    ``int` `x = digits[index - 1]; ` `    ``int` `greatest = index; ` `    ``for` `(``int` `i = index + 1; i < l; i += 1) { ` `        ``if` `(digits[i] < x && digits[i] > digits[greatest]) ` `            ``greatest = i; ` `    ``} ` `    ``// Swap the above found greatest digit with digits[index-1] ` `    ``swap(&digits[greatest], &digits[index - 1]); ` ` `  `    ``// Sort the digits after (index-1) in descending order ` `    ``sort(digits + index, digits + l); ` `    ``reverse(digits + index, digits + l); ` ` `  `    ``cout << ``"Immediate Smaller No. is "` `<< digits << endl; ` ` `  `    ``return``; ` `} ` ` `  `int` `main() ` `{ ` `    ``char` `digits[] = ``"2536"``; ` `    ``int` `l = ``strlen``(digits); ` `    ``immediateSmallest(digits, l); ` `    ``return` `0; ` `} `

## Java

 `// Java program for immediate smallest number ` `// after re-arranging the digits of a given number ` `import` `java.util.Arrays; ` `class` `GfG { ` `    ``// Function to sort a string in descending order ` `    ``public` `static` `String ReverseSort(String inputString) ` `    ``{ ` `        ``// convert input string to char array ` `        ``char` `tempArray[] = inputString.toCharArray(); ` `        ``// sort tempArray ` `        ``Arrays.sort(tempArray); ` `        ``// return new reverse sorted string ` `        ``String ans = ``""``; ` `        ``for` `(``int` `i = tempArray.length - ``1``; i >= ``0``; i--) { ` `            ``ans += tempArray[i]; ` `        ``} ` `        ``return` `ans; ` `    ``} ` `    ``static` `void` `immediateSmallest(String s) ` `    ``{ ` `        ``int` `l = s.length(); ` `        ``char` `ch[] = s.toCharArray(); ` `        ``// Find the last digit which is smaller ` `        ``// than the previous digit ` `        ``int` `index = -``1``; ` `        ``for` `(``int` `i = l - ``1``; i > ``0``; i--) { ` `            ``if` `(ch[i] < ch[i - ``1``]) { ` `                ``index = i; ` `                ``break``; ` `            ``} ` `        ``} ` `        ``// If no such digit is found, then all digits are in ascending order ` `        ``// means there cannot be a greater number with same set of digits ` `        ``if` `(index == -``1``) { ` `            ``System.out.println(``"Not Possible"``); ` `            ``return``; ` `        ``} ` ` `  `        ``// Find the greatest digit on right side of (index-1)'th digit that is ` `        ``// smaller than number[index-1] ` `        ``char` `c = ch[index - ``1``]; ` `        ``int` `x = Character.getNumericValue(c); ` `        ``int` `greatest = index; ` `        ``for` `(``int` `i = index + ``1``; i < l; i++) { ` `            ``char` `c1 = ch[i]; ` `            ``char` `c2 = ch[greatest]; ` `            ``int` `current = Character.getNumericValue(c1); ` `            ``int` `max_so_far = Character.getNumericValue(c2); ` `            ``if` `(current < x && current > max_so_far) { ` `                ``greatest = i; ` `            ``} ` `        ``} ` ` `  `        ``// Swap the above found greatest digit with digits[index-1] ` `        ``char` `temp = ``'g'``; ` `        ``temp = ch[index - ``1``]; ` `        ``ch[index - ``1``] = ch[greatest]; ` `        ``ch[greatest] = temp; ` ` `  `        ``String new_string = ``new` `String(ch); ` `        ``String left = new_string.substring(``0``, index); ` `        ``String right = new_string.substring(index, l); ` ` `  `        ``// Sort the digits after (index-1) in descending order ` `        ``String new_right = ReverseSort(right); ` ` `  `        ``String result = left + new_right; ` `        ``System.out.println(result); ` `        ``return``; ` `    ``} ` ` `  `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `n = ``2536``; ` `        ``String s = Integer.toString(n); ` `        ``immediateSmallest(s); ` `    ``} ` `} ` `// This code was contributed by Ankit Chawla `

## Python

 `# Python program for immediate smallest number  ` `# after re-arranging the digits of a given number  ` `   `  `def` `immediateSmallest(digits, l): ` `    ``# Find the last digit which is smaller ` `    ``# than previous digit ` `    ``index ``=``-``1` `    ``for` `i ``in` `range``(l``-``1``, ``0``, ``-``1``): ` `        ``if` `digits[i]digits[greatest]: ` `            ``greatest ``=` `i ` ` `  ` `  `    ``# Swap the above found greatest digit with digits[index-1]  ` ` `  `    ``(digits[greatest], digits[index``-``1``])``=``(digits[index``-``1``], digits[greatest]) ` ` `  `    ``# Sort the digits after (index-1) in descending order  ` ` `  `    ``left ``=` `digits[``0``:index] ` `    ``right ``=` `digits[index:] ` `    ``right ``=` `sorted``(right) ` `    ``ans ``=``"" ` `    ``for` `i ``in` `range``(``len``(left)): ` `        ``ans``+``=` `str``(left[i]) ` `    ``for` `i ``in` `range``(``len``(right)``-``1``, ``-``1``, ``-``1``): ` `        ``ans``+``=` `str``(right[i]) ` `    ``print` `ans ` ` `  `digits ``=``"2536"` `l ``=` `len``(digits) ` `digit_array ``=``[``0` `for` `i ``in` `range``(l)] ` `for` `i ``in` `range``(l): ` `    ``digit_array[i]``=` `int``(digits[i]) ` ` `  `immediateSmallest(digit_array, l) ` `# This code is contributed by Ankit Chawla `

## C#

 `// C# program for immediate smallest ` `// number after re-arranging the ` `// digits of a given number ` `class` `GFG { ` ` `  `    ``static` `void` `immediateSmallest(``string` `s) ` `    ``{ ` `        ``char` `temp = ``'g'``; ` ` `  `        ``// Find the last digit which is ` `        ``// greater han previous digit ` `        ``// and swap it with previous digit ` `        ``int` `l = s.Length; ` `        ``char``[] ch = s.ToCharArray(); ` `        ``for` `(``int` `i = l - 1; i > 0; i--) { ` `            ``if` `(ch[i] < ch[i - 1]) { ` `                ``temp = ch[i]; ` `                ``ch[i] = ch[i - 1]; ` `                ``ch[i - 1] = temp; ` `                ``string` `s1 = ``new` `string``(ch); ` `                ``System.Console.WriteLine(s1); ` `                ``return``; ` `            ``} ` `        ``} ` ` `  `        ``System.Console.WriteLine(``"Not Possible"``); ` `    ``} ` ` `  `    ``// Driver Code ` `    ``static` `void` `Main() ` `    ``{ ` `        ``int` `n = 3532; ` `        ``string` `s = System.Convert.ToString(n); ` `        ``immediateSmallest(s); ` `    ``} ` `} ` ` `  `// This code is contributed by mits `

## PHP

 ` 0; ``\$i``--) ` `    ``{  ` `        ``if` `(``\$s``[``\$i``] < ``\$s``[``\$i` `- 1]) ` `        ``{  ` `            ``\$temp` `= ``\$s``[``\$i``]; ` `            ``\$s``[``\$i``] = ``\$s``[ ``\$i` `- 1]; ` `            ``\$s``[``\$i` `- 1] = ``\$temp``; ` `            ``print``(``\$s``);  ` `            ``return``;  ` `        ``}  ` `    ``}  ` ` `  `    ``print``(``"Not Possible"``);  ` `}  ` ` `  `// Driver Code ` `\$n` `= 3532;  ` `\$s` `= ``strval``(``\$n``);  ` `immediateSmallest(``\$s``);  ` ` `  `// This code is contributed by mits ` `?> `

Output:

```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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.