Find smallest possible Number from a given large Number with same count of digits - GeeksforGeeks

20 Jan, 2022
Given a number K of length N, the task is to find the smallest possible number that can be formed from K of N digits by swapping the digits any number of times. 
Examples: 
Input: N = 15, K = 325343273113434 Output: 112233333344457 Explanation: The smallest number possible after swapping the digits of the given number is 112233333344457
Input: N = 7, K = 3416781 Output: 1134678 
Approach: The idea is to use Hashing. To implement the hash, an array arr[] of size 10 is created. The given number is iterated and the count of occurrence of every digit is stored in the hash at the corresponding index. Then iterate the hash array and print the ith digit according to its frequency. The output will be the smallest required number of N digits.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the above approach #include <iostream>using namespace std; // Function for finding the smallest// possible number after swapping// the digits any number of timesstring smallestPoss(string s, int n){    // Variable to store the final answer    string ans = "";     // Array to store the count of    // occurrence of each digit    int arr[10] = { 0 };     // Loop to calculate the number    // of occurrences of every digit    for (int i = 0; i < n; i++) {        arr[s[i] - 48]++;    }     // Loop to get smallest number    for (int i = 0; i < 10; i++) {        for (int j = 0; j < arr[i]; j++)            ans = ans + to_string(i);    }     // Returning the answer    return ans;} // Driver codeint main(){    int N = 15;    string K = "325343273113434";     cout << smallestPoss(K, N);     return 0;}
// Java implementation of the above approachclass GFG{ // Function for finding the smallest// possible number after swapping// the digits any number of timesstatic String smallestPoss(String s, int n){    // Variable to store the final answer    String ans = "";     // Array to store the count of    // occurrence of each digit    int arr[] = new int[10];     // Loop to calculate the number    // of occurrences of every digit    for (int i = 0; i < n; i++)    {        arr[s.charAt(i) - 48]++;    }     // Loop to get smallest number    for (int i = 0; i < 10; i++)    {        for (int j = 0; j < arr[i]; j++)            ans = ans + String.valueOf(i);    }     // Returning the answer    return ans;} // Driver codepublic static void main(String[] args){    int N = 15;    String K = "325343273113434";     System.out.print(smallestPoss(K, N));}} // This code is contributed by PrinciRaj1992
# Python3 implementation of the above approach # Function for finding the smallest# possible number after swapping# the digits any number of timesdef smallestPoss(s, n):         # Variable to store the final answer    ans = "";     # Array to store the count of    # occurrence of each digit    arr = [0]*10;     # Loop to calculate the number    # of occurrences of every digit    for i in range(n):        arr[ord(s[i]) - 48] += 1;         # Loop to get smallest number    for i in range(10):        for j in range(arr[i]):            ans = ans + str(i);         # Returning the answer    return ans; # Driver codeif __name__ == '__main__':    N = 15;    K = "325343273113434";     print(smallestPoss(K, N)); # This code is contributed by 29AjayKumar
// C# implementation of the above approachusing System; class GFG{ // Function for finding the smallest// possible number after swapping// the digits any number of timesstatic String smallestPoss(String s, int n){    // Variable to store the readonly answer    String ans = "";     // Array to store the count of    // occurrence of each digit    int []arr = new int[10];     // Loop to calculate the number    // of occurrences of every digit    for (int i = 0; i < n; i++)    {        arr[s[i] - 48]++;    }     // Loop to get smallest number    for (int i = 0; i < 10; i++)    {        for (int j = 0; j < arr[i]; j++)            ans = ans + String.Join("",i);    }     // Returning the answer    return ans;} // Driver codepublic static void Main(String[] args){    int N = 15;    String K = "325343273113434";     Console.Write(smallestPoss(K, N));}} // This code is contributed by PrinciRaj1992
<script> // Javascript implementation of the above approach // Function for finding the smallest// possible number after swapping// the digits any number of timesfunction smallestPoss(s, n){    // Variable to store the final answer    var ans = "";     // Array to store the count of    // occurrence of each digit    var arr = Array(10).fill(0);     // Loop to calculate the number    // of occurrences of every digit    for (var i = 0; i < n; i++) {        arr[s[i].charCodeAt(0) - 48]++;    }     // Loop to get smallest number    for (var i = 0; i < 10; i++) {        for (var j = 0; j < arr[i]; j++)            ans = ans + i.toString();    }     // Returning the answer    return ans;} // Driver codevar N = 15;var K = "325343273113434";document.write( smallestPoss(K, N)); </script>
112233333344457
 
Time Complexity: O(N)
Auxiliary Space: O(N + 10)
princiraj1992
29AjayKumar
itsok
sweetyty
subhammahato348
large-numbers
number-digits
Arrays
Hash
Mathematical
Arrays
Hash
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Count pairs with given sum
Chocolate Distribution Problem
Window Sliding Technique
Reversal algorithm for array rotation
Next Greater Element
Internal Working of HashMap in Java
Count pairs with given sum
Hashing | Set 1 (Introduction)
Hashing | Set 3 (Open Addressing)
Hashing | Set 2 (Separate Chaining)