Generate a number such that the frequency of each digit is digit times the frequency in given number - GeeksforGeeks

21 Apr, 2021
Given a number N containing digits from 1 to 9 only. The task is to generate a new number using the number N such that the frequency of each digit in the new number is equal to the frequency of that digit in N multiplied by the digit itself.Note: The digits in the new number must be in increasing order.Examples:  
Input : N = 312 Output : 122333 Explanation : The output contains digit 1 once, digit 2 twice and digit 3 thrice.Input : N = 525 Output : 225555555555 Explanation : The output contains digit 2 twice and digit 5 ten times. 5 is ten times because its frequency is 2 in the given integer.  
 
The idea is to store the count or the frequency of the digits in the given number N using a counting array or hash. Now, for each digit add it to the new number, K number of times where K is equal to its frequency in the counting array multiplied by the digit itself.Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// CPP program to print a number such that the//  frequency of each digit in the new number is// is equal to its frequency in the given number// multiplied by the digit itself. #include <bits/stdc++.h>using namespace std; // Function to print such a numbervoid printNumber(int n){    // initializing a hash array    int count[10] = { 0 };     // counting frequency of the digits    while (n) {        count[n % 10]++;        n /= 10;    }     // printing the new number    for (int i = 1; i < 10; i++) {        for (int j = 0; j < count[i] * i; j++)            cout << i;    }} // Driver codeint main(){    int n = 3225;     printNumber(n);     return 0;}
// Java program to print a number such that the// frequency of each digit in the new number is// is equal to its frequency in the given number// multiplied by the digit itself. import java.io.*; class GFG {  // Function to print such a numberstatic void printNumber(int n){    // initializing a hash array    int count[] = new int[10];     // counting frequency of the digits    while (n>0) {        count[n % 10]++;        n /= 10;    }     // printing the new number    for (int i = 1; i < 10; i++) {        for (int j = 0; j < count[i] * i; j++)            System.out.print(i);    }} // Driver code     public static void main (String[] args) {        int n = 3225;     printNumber(n);    }}// This code is contributed by inder_verma
# Python 3 program to print a number such that the# frequency of each digit in the new number is# is equal to its frequency in the given number# multiplied by the digit itself. # Function to print such a numberdef printNumber(n):     # initializing a hash array    count = [0]*10     # counting frequency of the digits    while (n) :        count[n % 10] += 1        n //= 10     # printing the new number    for i in range(1,10) :        for j in range(count[i] * i):            print(i,end="") # Driver codeif __name__ == "__main__":    n = 3225     printNumber(n)     # This code is contributed by# ChitraNayal
// C# program to print a number such// that the frequency of each digit// in the new number is equal to its// frequency in the given number// multiplied by the digit itself.using System; class GFG{ // Function to print such a numberstatic void printNumber(int n){    // initializing a hash array    int []count = new int[10];     // counting frequency of    // the digits    while (n > 0)    {        count[n % 10]++;        n /= 10;    }     // printing the new number    for (int i = 1; i < 10; i++)    {        for (int j = 0;                 j < count[i] * i; j++)            Console.Write(i);    }} // Driver codepublic static void Main (){    int n = 3225;     printNumber(n);}} // This code is contributed// by inder_verma
<?php// PHP program to print a number such// that the frequency of each digit// in the new number is equal to its// frequency in the given number// multiplied by the digit itself. // Function to print such a numberfunction printNumber($n){    // initializing a hash array    $count = array();    for($i = 0; $i<= 10; $i++)        $count[$i] = 0;             // counting frequency of the digits    while ($n)    {        $count[$n % 10]++;        $n /= 10;    }     // printing the new number    for ($i = 1; $i < 10; $i++)    {        for ($j = 0;             $j < $count[$i] * $i; $j++)            echo $i;    }} // Driver code$n = 3225; printNumber($n); // This code is contributed// by Akanksha Rai(Abby_akku)
<script> // JavaScript program to print// a number such that the// frequency of each digit// in the new number is// is equal to its frequency// in the given number// multiplied by the digit itself.  // Function to print such a numberfunction printNumber(n){    // initializing a hash array    let count = new Uint8Array(10);     // counting frequency of the digits    while (n) {        count[n % 10]++;        n = Math.floor(n / 10);    }     // printing the new number    for (let i = 1; i < 10; i++) {        for (let j = 0; j < count[i] * i; j++)            document.write(i);    }} // Driver code     let n = 3225;     printNumber(n);  // This code is contributed by Surbhi Tyagi. </script>
222233355555
 
inderDuMCA
vt_m
ukasp
Akanksha_Rai
surbhityagi15
frequency-counting
number-digits
C++ Programs
Competitive Programming
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Passing a function as a parameter in C++
Program to implement Singly Linked List in C++ using class
Const keyword in C++
cout in C++
Dynamic _Cast in C++
Competitive Programming - A Complete Guide
Practice for cracking any coding interview
Arrow operator -> in C/C++ with Examples
Top 15 Websites for Coding Challenges and Competitions
Modulo 10^9+7 (1000000007)