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

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 number
void 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 code
int main()
{
    int n = 3225;
  
    printNumber(n);
  
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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[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 code
public static void Main () 
{
    int n = 3225;
  
    printNumber(n);
}
}
  
// This code is contributed
// by inder_verma
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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 number
function 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)
chevron_right

Output:
222233355555




Recommended Posts:


Coder Machine Learner Social Activist Vocalist

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :