Construct a frequency array of digits of the values obtained from x^1, x^2, …….., x^n
Last Updated :
05 Sep, 2022
Given are two integers (x and n). The task is to find an array such that it contains the frequency of index numbers occurring in (x^1, x^2, …., x^(n-1), x^(n) ).
Examples:
Input: x = 15, n = 3
Output: 0 1 2 2 0 3 0 1 0 0
Numbers x^1 to x^n are 15, 225, 3375.
So frequency array is 0 1 2 2 0 3 0 1 0 0.
Input: x = 1, n = 5
Output: 0 5 0 0 0 0 0 0 0 0
Numbers x^1 to x^n are 1, 1, 1, 1, 1.
So frequency of digits is 0 5 0 0 0 0 0 0 0 0.
Approach:
- Maintain a frequency count array to store the count of digits 0-9.
- Traverse through each digit from x^1 to x^n, for each digit add 1 to the corresponding index in the frequency count array.
- Print the frequency array
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void countDigits( double val, long arr[])
{
while (( long )val > 0) {
long digit = ( long )val % 10;
arr[( int )digit]++;
val = ( long )val / 10;
}
return ;
}
void countFrequency( int x, int n)
{
long freq_count[10]={0};
for ( int i = 1; i <= n; i++)
{
double val = pow (( double )x, ( double )i);
countDigits(val, freq_count);
}
for ( int i = 0; i <= 9; i++)
{
cout << freq_count[i] << " " ;
}
}
int main()
{
int x = 15, n = 3;
countFrequency(x, n);
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
static void countDigits( double val, long [] arr)
{
while (( long )val > 0 ) {
long digit = ( long )val % 10 ;
arr[( int )digit]++;
val = ( long )val / 10 ;
}
return ;
}
static void countFrequency( int x, int n)
{
long [] freq_count = new long [ 10 ];
for ( int i = 1 ; i <= n; i++) {
double val = Math.pow(( double )x, ( double )i);
countDigits(val, freq_count);
}
for ( int i = 0 ; i <= 9 ; i++) {
System.out.print(freq_count[i] + " " );
}
}
public static void main(String args[])
{
int x = 15 , n = 3 ;
countFrequency(x, n);
}
}
|
Python 3
import math
def countDigits(val, arr):
while (val > 0 ) :
digit = val % 10
arr[ int (digit)] + = 1
val = val / / 10
return ;
def countFrequency(x, n):
freq_count = [ 0 ] * 10
for i in range ( 1 , n + 1 ) :
val = math. pow (x, i)
countDigits(val, freq_count)
for i in range ( 10 ) :
print (freq_count[i], end = " " );
if __name__ = = "__main__" :
x = 15
n = 3
countFrequency(x, n)
|
C#
using System;
class GFG
{
static void countDigits( double val,
long [] arr)
{
while (( long )val > 0)
{
long digit = ( long )val % 10;
arr[( int )digit]++;
val = ( long )val / 10;
}
return ;
}
static void countFrequency( int x, int n)
{
long [] freq_count = new long [10];
for ( int i = 1; i <= n; i++)
{
double val = Math.Pow(( double )x,
( double )i);
countDigits(val, freq_count);
}
for ( int i = 0; i <= 9; i++)
{
Console.Write(freq_count[i] + " " );
}
}
public static void Main()
{
int x = 15, n = 3;
countFrequency(x, n);
}
}
|
PHP
<?php
function countDigits( $val , & $arr )
{
while ( $val > 0)
{
$digit = $val % 10;
$arr [(int)( $digit )] += 1;
$val = (int)( $val / 10);
}
return ;
}
function countFrequency( $x , $n )
{
$freq_count = array_fill (0, 10, 0);
for ( $i = 1; $i < $n + 1; $i ++)
{
$val = pow( $x , $i );
countDigits( $val , $freq_count );
}
for ( $i = 0; $i < 10; $i ++)
{
echo $freq_count [ $i ] . " " ;
}
}
$x = 15;
$n = 3;
countFrequency( $x , $n )
?>
|
Javascript
<script>
function countDigits(val,arr)
{
while (val > 0) {
let digit = val % 10;
arr[digit]++;
val = Math.floor(val / 10);
}
return ;
}
function countFrequency(x,n)
{
let freq_count = new Array(10);
for (let i=0;i<10;i++)
{
freq_count[i]=0;
}
for (let i = 1; i <= n; i++) {
let val = Math.pow(x, i);
countDigits(val, freq_count);
}
for (let i = 0; i <= 9; i++) {
document.write(freq_count[i] + " " );
}
}
let x = 15, n = 3;
countFrequency(x, n);
</script>
|
Output
0 1 2 2 0 3 0 1 0 0
Complexity Analysis:
- Time complexity: O(nlogn) since using a pow function “logn time complexity” inside a for loop
- Auxiliary Space: O(10)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...