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 : 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 :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for immediate smallest number
// after re-arranging the digits of a given number
  
#include <bits/stdc++.h>
using namespace std;
  
void immediateSmallest(string s)
{
  
    // Find the last digit which is greater
    // than previous digit and swap it with
    // previous digit
    int l = s.size();
    for (int i = l - 1; i > 0; i--) {
        if (s[i] < s[i - 1]) {
            swap(s[i], s[i - 1]);
            cout << s;
            return;
        }
    }
  
    cout << "Not Possible\n";
}
  
int main()
{
    int n = 3532;
    string s = to_string(n);
    immediateSmallest(s);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java 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 
    // than 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.out.println(s1); 
            return
        
    
  
    System.out.println("Not Possible"); 
  
public static void main(String[] args) 
    int n = 3532
    String s = Integer.toString(n); 
    immediateSmallest(s); 
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for immediate smallest
# number after re-arranging the 
# digits of a given number
  
def immediateSmallest(str): 
    temp = "g"
    s = list(str
      
    # Find the last digit which is 
    # greater than previous digit 
    # and swap it with previous digit 
    l = len(s)
    for i in range(l - 1, 0, -1):
        if (s[i] < s[i - 1]): 
            temp = s[i]
            s[i] = s[ i - 1]
            s[i - 1] = temp
            print(''.join(s)) 
            return
    print("Not Possible"
  
# Driver Code
n = 3532
s = str(3532
immediateSmallest(s)
  
# This code is contributed by Rajput-Ji

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program for immediate smallest
// number after re-arranging the 
// digits of a given number
  
function immediateSmallest($s
    $temp = "g";
  
    // Find the last digit which is 
    // greater han previous digit 
    // and swap it with previous digit 
    $l = strlen($s);
    for ($i = $l - 1; $i > 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
?>

chevron_right



Output:

3523


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.