Related Articles

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

• Last Updated : 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++

 `// 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 ``using` `namespace` `std;` `// Function to print such a number``void` `printNumber(``int` `n)``{``    ``// initializing a hash array``    ``int` `count = { 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 code``int` `main()``{``    ``int` `n = 3225;` `    ``printNumber(n);` `    ``return` `0;``}`

## Java

 `// 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 number``static` `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`

## Python3

 `# 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 number``def` `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 code``if` `__name__ ``=``=` `"__main__"``:``    ``n ``=` `3225` `    ``printNumber(n)``    ` `# This code is contributed by``# ChitraNayal`

## C#

 `// 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 number``static` `void` `printNumber(``int` `n)``{``    ``// initializing a hash array``    ``int` `[]count = ``new` `int``;` `    ``// 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 code``public` `static` `void` `Main ()``{``    ``int` `n = 3225;` `    ``printNumber(n);``}``}` `// This code is contributed``// by inder_verma`

## PHP

 `

## Javascript

 ``
Output:
`222233355555` My Personal Notes arrow_drop_up