Find smallest permutation of given number - GeeksforGeeks

04 May, 2021
Given a long integer, return the smallest(magnitude) integer permutation of that number.Examples:  
Input : 5468001
Output : 1004568

Input : 5341
Output : 1345
 
Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. 
 
Question Source : GE digital Interview Experience | Set 6We have already discussed a solution in below post.Smallest number by rearranging digits of a given numberIn this post, a different approach is discussed.Approach : As number is long, store the number as string, sort the string, if there is no leading zero, return this string, if there is any leading zero, swap first element of string with first non-zero element of string, and return the string.Below is the implementation of above approach :  
C++
Java
Python
C#
PHP
Javascript
// CPP program to find smallest// permutation of given number#include <bits/stdc++.h>using namespace std; // return the smallest number permutationstring findSmallestPermutation(string s){    int len = s.length();     // sort the string    sort(s.begin(), s.end());     // check for leading zero in string    // if there are any leading zeroes,    // swap the first zero with first non-zero number    int i = 0;    while (s[i] == '0')        i++;         swap(s[0], s[i]);    return s;} // driver programint main(){    // take number input in string    string s = "5468001";    string res = findSmallestPermutation(s);    cout << res << endl;    return 0;}
// Java program to find smallest// permutation of given numberimport java.util.Arrays; public class GFG {         // return the smallest number permutation    static char[] findSmallestPermutation(String s1)    {        // sort the string        char s[] = s1.toCharArray();        Arrays.sort(s);              // check for leading zero in string        // if there are any leading zeroes,        // swap the first zero with first non-zero        // number        int i = 0;        while (s[i] == '0')            i++;                  char temp = s[0];        s[0] = s[i];        s[i] = temp;        return s;    }          // driver program    public static void main(String args[])    {        // take number input in string        String s = "5468001";        char res[] = findSmallestPermutation(s);        System.out.println(res);    }}// This code is contributed by Sumit Ghosh
# Python program to find smallest# permutation of given number # Sort functiondef sort_string(a):    return ''.join(sorted(a)) # return the smallest number permutationdef findSmallestPermutation(s):      # sort the string    s = sort_string(s)      # check for leading zero in string    # if there are any leading zeroes,    # swap the first zero with first non-zero number    i = 0    while (s[i] == '0'):        i += 1    a = list(s)    temp = a[0]    a[0] = a[i]    a[i] = temp    s = "".join(a)    return s  # driver program # take number input in strings = "5468001"res = findSmallestPermutation(s)print res # This code is contributed by Sachin Bisht
// C# program to find smallest// permutation of given numberusing System; public class GFG {         // return the smallest number permutation    static char[] findSmallestPermutation(string s1)    {                 // sort the string        char []s = s1.ToCharArray();;        Array.Sort(s);             // check for leading zero in string        // if there are any leading zeroes,        // swap the first zero with first non-zero        // number        int i = 0;        while (s[i] == '0')            i++;                 char temp = s[0];        s[0] = s[i];        s[i] = temp;        return s;    }         // driver program    public static void Main()    {                 // take number input in string        string s = "5468001";                 char []res = findSmallestPermutation(s);        Console.WriteLine(res);    }} // This code is contributed by vt_m.
<?php// PHP program to find smallest// permutation of given number // return the smallest// number permutationfunction findSmallestPermutation($s){    $len = strlen($s);    $s = str_split($s);         // sort the string    sort($s);     // check for leading zero    // in string if there are    // any leading zeroes,    // swap the first zero with    // first non-zero number    $i = 0;    while ($s[$i] == '0')        $i++;         $tmp = $s[0];    $s[0] = $s[$i];    $s[$i] = $tmp;    $s=implode("", $s);    return $s;} // Driver Code // take number// input in string$s = "5468001";$res = findSmallestPermutation($s);echo $res; // This code is contributed// by mits.?>
// Javascript program to find smallest// permutation of given number // return the smallest// number permutationfunction findSmallestPermutation(s){    let len = s.length;    s = s.split("");         // sort the string    s = s.sort();     // check for leading zero    // in string if there are    // any leading zeroes,    // swap the first zero with    // first non-zero number    let i = 0;    while (s[i] == '0')        i++;         let tmp = s[0];    s[0] = s[i];    s[i] = tmp;    s=  s.join("");    return s;} // Driver Code // take number// input in stringlet s = "5468001";let res = findSmallestPermutation(s);document.write(res); // This code is contributed// by _saurabh_jaiswal
Output:  
1004568
Optimization : Since character set is limited (‘0’ to ‘9’), we can write our own sort method that works in linear time (by counting frequencies of all characters)This article is contributed by Mandeep Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
vt_m
Mithun Kumar
pandeyprince25
_saurabh_jaiswal
GE
number-digits
Mathematical
GE
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to find sum of elements in a given array
Operators in C / C++
The Knight's tour problem | Backtracking-1
Euclidean algorithms (Basic and Extended)
Find minimum number of coins that make a given value
Print all possible combinations of r elements in a given array of size n
Write a program to calculate pow(x,n)
Algorithm to solve Rubik's Cube
Write a program to reverse digits of a number
How to swap two numbers without using a temporary variable?