text: Find smallest possible Number from a given large Number with same count of digits | C ++ implementation of the above approach ; Function for finding the smallest possible number after swapping the digits any number of times ; Variable to store the final answer ; Array to store the count of occurrence of each digit ; Loop to calculate the number of occurrences of every digit ; Loop to get smallest number ; Returning the answer ; Driver code
code: #include <iostream> NEW_LINE using namespace std ; string smallestPoss ( string s , int n ) { string ans = " " ; int arr [ 10 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { arr [ s [ i ] - 48 ] ++ ; } for ( int i = 0 ; i < 10 ; i ++ ) { for ( int j = 0 ; j < arr [ i ] ; j ++ ) ans = ans + to_string ( i ) ; } return ans ; } int main ( ) { int N = 15 ; string K = "325343273113434" ; cout << smallestPoss ( K , N ) ; return 0 ; }