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;
void printNumber( int n)
{
map< int , int > freq;
while (n > 0) {
freq[n % 10]++;
n /= 10;
}
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(output.begin(), output.end());
cout << output;
}
int main()
{
int n = 3225;
printNumber(n);
return 0;
}
|
Java
import java.util.*;
public class Main
{
public static void printNumber( int n)
{
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 ;
}
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));
}
}
char [] arr = output.toString().toCharArray();
Arrays.sort(arr);
System.out.println( new String(arr));
}
public static void main(String[] args) {
int n = 3225 ;
printNumber(n);
}
}
|
Python3
def printNumber(n):
freq = {}
while n > 0 :
freq[n % 10 ] = freq.get(n % 10 , 0 ) + 1
n / / = 10
output = ""
for i in range ( 1 , 10 ):
if freq.get(i, 0 ) > 0 :
for j in range (freq[i] * i):
output + = str (i)
print (''.join( sorted (output)))
n = 3225
printNumber(n)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void PrintNumber( int n)
{
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;
}
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();
}
}
}
Console.WriteLine(output);
}
static void Main( string [] args)
{
int n = 3225;
PrintNumber(n);
}
}
|
Javascript
function printNumber(n) {
let freq = new Map();
while (n > 0) {
freq.set(n % 10, (freq.get(n % 10) || 0) + 1);
n = Math.floor(n / 10);
}
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();
}
}
}
output = output.split( '' ).sort().join( '' );
console.log(output);
}
let n = 3225;
printNumber(n);
|
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++
#include <bits/stdc++.h>
using namespace std;
void printNumber( int n)
{
int count[10] = { 0 };
while (n) {
count[n % 10]++;
n /= 10;
}
for ( int i = 1; i < 10; i++) {
for ( int j = 0; j < count[i] * i; j++)
cout << i;
}
}
int main()
{
int n = 3225;
printNumber(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void printNumber( int n)
{
int count[] = new int [ 10 ];
while (n> 0 ) {
count[n % 10 ]++;
n /= 10 ;
}
for ( int i = 1 ; i < 10 ; i++) {
for ( int j = 0 ; j < count[i] * i; j++)
System.out.print(i);
}
}
public static void main (String[] args) {
int n = 3225 ;
printNumber(n);
}
}
|
Python3
def printNumber(n):
count = [ 0 ] * 10
while (n) :
count[n % 10 ] + = 1
n / / = 10
for i in range ( 1 , 10 ) :
for j in range (count[i] * i):
print (i,end = "")
if __name__ = = "__main__" :
n = 3225
printNumber(n)
|
C#
using System;
class GFG
{
static void printNumber( int n)
{
int []count = new int [10];
while (n > 0)
{
count[n % 10]++;
n /= 10;
}
for ( int i = 1; i < 10; i++)
{
for ( int j = 0;
j < count[i] * i; j++)
Console.Write(i);
}
}
public static void Main ()
{
int n = 3225;
printNumber(n);
}
}
|
PHP
<?php
function printNumber( $n )
{
$count = array ();
for ( $i = 0; $i <= 10; $i ++)
$count [ $i ] = 0;
while ( $n )
{
$count [ $n % 10]++;
$n /= 10;
}
for ( $i = 1; $i < 10; $i ++)
{
for ( $j = 0;
$j < $count [ $i ] * $i ; $j ++)
echo $i ;
}
}
$n = 3225;
printNumber( $n );
|
Javascript
<script>
function printNumber(n)
{
let count = new Uint8Array(10);
while (n) {
count[n % 10]++;
n = Math.floor(n / 10);
}
for (let i = 1; i < 10; i++) {
for (let j = 0; j < count[i] * i; j++)
document.write(i);
}
}
let n = 3225;
printNumber(n);
</script>
|
Time Complexity : O(log10n), where n is the given integer.
Auxiliary Space : O(1), no extra space required so it is a constant.
Please Login to comment...