[GE][First Method]Find smallest permutation of given number










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

Input : 5341
Output : 1345
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 :

// CPP program to find smallest
// permutation of given number
#include <bits/stdc++.h>
using namespace std;
  
// return the smallest number permutation
string 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 program
int main()
{
    // take number input in string
    string s = "5468001";
    string res = findSmallestPermutation(s);
    cout << res << endl;
    return 0;
}

Output:
1004568


// Java program to find smallest
// permutation of given number
import 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

Output:
1004568



# Python program to find smallest
# permutation of given number
  
# Sort function
def sort_string(a):
    return ''.join(sorted(a))
  
# return the smallest number permutation
def findSmallestPermutation(s):
    le = len(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)
    a[0] = a[i]
    a[i] = '0'
    s = "".join(a)
    return s
   
# driver program
  
# take number input in string
s = "5468001"
res = findSmallestPermutation(s)
print res
  
# This code is contributed by Sachin Bisht

Output:
1004568


















Comments

Popular posts from this blog

[16 Feb 2020] Given an array where every element occurs three times, except one element which occurs only once.

Which data structure is used in redo-undo feature?

Important Program Collection