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.Â
Â
Â
Brute Force Approach:
We can use a map to store the frequency of each digit in the given number. Then, for each digit from 1 to 9, we can append that digit to the output string as many times as its frequency multiplied by the digit itself. Finally, we can sort the output string in increasing order and print it.
Below is the implementation of the above approach:Â
C++
#include <bits/stdc++.h>using namespace std;Â
// Function to print such a numbervoid printNumber(int n){    // Count the frequency of each digit    map<int, int> freq;    while (n > 0) {        freq[n % 10]++;        n /= 10;    }Â
    // Construct the output string    string output = "";    for (int i = 1; i <= 9; i++) {        if (freq[i] > 0) {            for (int j = 0; j < freq[i] * i; j++)                output += to_string(i);        }    }Â
    // Sort and print the output string    sort(output.begin(), output.end());    cout << output;}Â
// Driver codeint main(){Â Â Â Â int n = 3225;Â Â Â Â printNumber(n);Â Â Â Â return 0;} |
Java
import java.util.*;Â
public class Main {Â
  // Function to print such a number  public static void printNumber(int n)  {Â
    // Count the frequency of each digit    Map<Integer, Integer> freq = new HashMap<Integer, Integer>();    while (n > 0) {      int digit = n % 10;      freq.put(digit, freq.getOrDefault(digit, 0) + 1);      n /= 10;    }Â
    // Construct the output string    StringBuilder output = new StringBuilder();    for (int i = 1; i <= 9; i++) {      if (freq.containsKey(i) && freq.get(i) > 0) {        for (int j = 0; j < freq.get(i) * i; j++)          output.append(Integer.toString(i));      }    }Â
    // Sort and print the output string    char[] arr = output.toString().toCharArray();    Arrays.sort(arr);    System.out.println(new String(arr));  }Â
  // Driver code  public static void main(String[] args) {    int n = 3225;    printNumber(n);  }} |
Python3
# Function to print such a numberdef printNumber(n):    # Count the frequency of each digit    freq = {}    while n > 0:        freq[n % 10] = freq.get(n % 10, 0) + 1        n //= 10Â
    # Construct the output string    output = ""    for i in range(1, 10):        if freq.get(i, 0) > 0:            for j in range(freq[i] * i):                output += str(i)Â
    # Sort and print the output string    print(''.join(sorted(output)))Â
Â
# Driver coden = 3225printNumber(n) |
C#
using System;using System.Collections.Generic;using System.Linq;Â
class Program {Â
    // Function to print such a number    static void PrintNumber(int n)    {        // Count the frequency of each digit        Dictionary<int, int> freq            = new Dictionary<int, int>();        while (n > 0) {            if (freq.ContainsKey(n % 10)) {                freq[n % 10]++;            }            else {                freq.Add(n % 10, 1);            }            n /= 10;        }Â
        // Construct the output string        string output = "";        for (int i = 1; i <= 9; i++) {            if (freq.ContainsKey(i) && freq[i] > 0) {                for (int j = 0; j < freq[i] * i; j++) {                    output += i.ToString();                }            }        }Â
        // Sort and print the output string        Console.WriteLine(output);    }Â
    // Driver code    static void Main(string[] args)    {        int n = 3225;        PrintNumber(n);    }} |
Javascript
// Function to print such a numberfunction printNumber(n) {    // Count the frequency of each digit    let freq = new Map();    while (n > 0) {        freq.set(n % 10, (freq.get(n % 10) || 0) + 1);        n = Math.floor(n / 10);    }Â
    // Construct the output string    let output = "";    for (let i = 1; i <= 9; i++) {        if (freq.get(i) > 0) {            for (let j = 0; j < freq.get(i) * i; j++) {                output += i.toString();            }        }    }Â
    // Sort and print the output string    output = output.split('').sort().join('');    console.log(output);}Â
// Driver codelet n = 3225;printNumber(n); |
222233355555
Time Complexity: O(N LOG N)
Auxiliary Space: O(N)
Approach:
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 <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
// 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 |
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 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#
// 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// 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) |
Javascript
<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
Time Complexity : O(log10n), where n is the given integer.
Auxiliary Space : O(1), no extra space required so it is a constant.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



