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 <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; } |
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[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 |
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 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) |
222233355555
Recommended Posts:
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Check if frequency of each digit is less than the digit
- Convert a number of length N such that it contains any one digit at least 'K' times
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Number of triplets such that each value is less than N and each pair sum is a multiple of K
- Smallest integer greater than n such that it consists of digit m exactly k times
- Count of Binary Strings of length N such that frequency of 1's exceeds frequency of 0's
- Count number of times each Edge appears in all possible paths of a given Tree
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Times required by Simple interest for the Principal to become Y times itself
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Count of N-digit numbers having digit XOR as single digit
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Largest index for each distinct character in given string with frequency K
- Calculate the frequency of each word in the given string
- Check if frequency of each element in given array is unique or not
- Longest subsequence where each character occurs at least k times
- Count substrings with each character occurring at most k times
- Number formed by adding product of its max and min digit K times
- Minimum N-Digit number required to obtain largest N-digit number after performing given operations
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.


