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



Smallest number by rearranging digits of a given number

Find the Smallest number (Not leading Zeros) which can be obtained by rearranging the digits of given number.
Examples:
Input: n = 846903
Output: 304689

Input: n = 55010
Output: 10055



Steps to find the smallest number.


  1. Count the frequency of each digit in the number.
  2. Place the smallest digit (except 0) at the left most of required number.
    and decrement the frequency of that digit by 1.
  3. Place all remaining digits in ascending order from left to right.
This solution is based on counting sort.



// C++ program for finding smallest number
// from digits of given number
#include<iostream>
using namespace std;
  
// function to find the smallest number
int smallest(int num)
{
    // initialize frequency of each digit to Zero
    int freq[10] = {0};
  
    // count frequency of each digit in the number
    while (num)
    {
        int d = num % 10; // extract last digit
        freq[d]++; // increment counting
        num = num / 10; //remove last digit
    }
  
    // Set the LEFTMOST digit to minimum expect 0
    int result = 0;
    for (int i = 1 ; i <= 9 ; i++)
    {
        if (freq[i])
        {
            result = i;
            freq[i]--;
            break;
        }
    }
  
    // arrange all remaining digits
    // in ascending order
    for (int i = 0 ; i <= 9 ; i++)
        while (freq[i]--)
            result = result * 10 + i;
  
    return result;
}
  
// Driver Program
int main()
{
    int num = 570107;
    cout << smallest(num);
    return 0;
}

Output:
100577


// Java program for finding smallest number
// from digits of given number
public class GFG {
  
    // function to find the smallest number
    static int smallest(int num)
    {
        // initialize frequency of each digit to Zero
        int[] freq = new int[10];
       
        // count frequency of each digit in the number
        while (num > 0)
        {
            int d = num % 10; // extract last digit
            freq[d]++; // increment counting
            num = num / 10; //remove last digit
        }
       
        // Set the LEFTMOST digit to minimum expect 0
        int result = 0;
        for (int i = 1 ; i <= 9 ; i++)
        {
            if (freq[i] != 0)
            {
                result = i;
                freq[i]--;
                break;
            }
        }
       
        // arrange all remaining digits
        // in ascending order
        for (int i = 0 ; i <= 9 ; i++)
            while (freq[i]-- != 0)
                result = result * 10 + i;
       
        return result;
    }
       
    // Driver Program
    public static void main(String args[])
    {
        int num = 570107;
        System.out.println(smallest(num));
    }
}
// This code is contributed by Sumit Ghosh

Output:
100577



















































Comments

Popular posts from this blog

Important Program Collection

[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?