Skip to content
Related Articles
Construct a frequency array of digits of the values obtained from x^1, x^2, …….., x^n
• Difficulty Level : Easy
• Last Updated : 30 Nov, 2018

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.
```

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. Maintain a frequency count array to store the count of digits 0-9.
2. Traverse through each digit of x^1 to x^n, for each digit add 1 to corresponding index in frequency count array.
3. Print the frequency array

Below is the implementation of above approach:

C++

 `// CPP implementation of above approach``#include``using` `namespace` `std;`` ` `// Function that traverses digits in a number and``// modifies frequency count array``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)``{`` ` `    ``// Array to keep count of digits``    ``long` `freq_count[10]={0};`` ` `    ``// Traversing through x^1 to x^n``    ``for` `(``int` `i = 1; i <= n; i++)``    ``{``        ``// For power function, both its parameters are``        ``// to be in double``        ``double` `val = ``pow``((``double``)x, (``double``)i);``        ``// calling countDigits function on x^i``        ``countDigits(val, freq_count);``    ``}`` ` `    ``// Printing count of digits 0-9``    ``for` `(``int` `i = 0; i <= 9; i++) ``    ``{``        ``cout << freq_count[i] <<  ``" "``;``    ``}``}``// Driver code``int` `main()``{``    ``int` `x = 15, n = 3;``    ``countFrequency(x, n);``}``// This code is contributed by ihritik`

Java

 `// Java implementation of above approach``import` `java.io.*;``import` `java.util.*;``public` `class` `GFG {`` ` `    ``// Function that traverses digits in a number and``    ``// modifies frequency count array``    ``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)``    ``{`` ` `        ``// Array to keep count of digits``        ``long``[] freq_count = ``new` `long``[``10``];`` ` `        ``// Traversing through x^1 to x^n``        ``for` `(``int` `i = ``1``; i <= n; i++) {``            ``// For power function, both its parameters are``            ``// to be in double``            ``double` `val = Math.pow((``double``)x, (``double``)i);``            ``// calling countDigits function on x^i``            ``countDigits(val, freq_count);``        ``}`` ` `        ``// Printing count of digits 0-9``        ``for` `(``int` `i = ``0``; i <= ``9``; i++) {``            ``System.out.print(freq_count[i] + ``" "``);``        ``}``    ``}``    ``// Driver code``    ``public` `static` `void` `main(String args[])``    ``{``        ``int` `x = ``15``, n = ``3``;``        ``countFrequency(x, n);``    ``}``}`

Python 3

 `# Python 3 implementation ``# of above approach``import` `math`` ` `# Function that traverses digits ``# in a number and modifies ``# frequency count array``def` `countDigits(val, arr):``     ` `    ``while` `(val > ``0``) :``        ``digit ``=` `val ``%` `10``        ``arr[``int``(digit)] ``+``=` `1``        ``val ``=` `val ``/``/` `10``         ` `    ``return``;`` ` `def` `countFrequency(x, n):``     ` `    ``# Array to keep count of digits``    ``freq_count ``=` `[``0``] ``*` `10`` ` `    ``# Traversing through x^1 to x^n``    ``for` `i ``in` `range``(``1``, n ``+` `1``) :``         ` `        ``# For power function, ``        ``# both its parameters ``        ``# are to be in double``        ``val ``=` `math.``pow``(x, i)``         ` `        ``# calling countDigits ``        ``# function on x^i``        ``countDigits(val, freq_count)``         ` `    ``# Printing count of digits 0-9``    ``for` `i ``in` `range``(``10``) :``        ``print``(freq_count[i], end ``=` `" "``);`` ` `# Driver code``if` `__name__ ``=``=` `"__main__"``:``     ` `    ``x ``=` `15``    ``n ``=` `3``    ``countFrequency(x, n)`` ` `# This code is contributed ``# by ChitraNayal`

C#

 `// C# implementation of above approach``using` `System;`` ` `class` `GFG ``{`` ` `// Function that traverses digits ``// in a number and modifies ``// frequency count array``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)``{`` ` `    ``// Array to keep count of digits``    ``long``[] freq_count = ``new` `long``[10];`` ` `    ``// Traversing through x^1 to x^n``    ``for` `(``int` `i = 1; i <= n; i++) ``    ``{``        ``// For power function, both its ``        ``// parameters are to be in double``        ``double` `val = Math.Pow((``double``)x,``                              ``(``double``)i);``                               ` `        ``// calling countDigits ``        ``// function on x^i``        ``countDigits(val, freq_count);``    ``}`` ` `    ``// Printing count of digits 0-9``    ``for` `(``int` `i = 0; i <= 9; i++) ``    ``{``        ``Console.Write(freq_count[i] + ``" "``);``    ``}``}`` ` `// Driver code``public` `static` `void` `Main()``{``    ``int` `x = 15, n = 3;``    ``countFrequency(x, n);``}``}`` ` `// This code is contributed ``// by Shashank`

PHP

 ` 0) ``    ``{``        ``\$digit` `= ``\$val` `% 10;``        ``\$arr``[(int)(``\$digit``)] += 1;``        ``\$val` `= (int)(``\$val` `/ 10);``    ``}``    ``return``;``}`` ` `function` `countFrequency(``\$x``, ``\$n``)``{``     ` `    ``// Array to keep count of digits``    ``\$freq_count` `= ``array_fill``(0, 10, 0);`` ` `    ``// Traversing through x^1 to x^n``    ``for` `(``\$i` `= 1; ``\$i` `< ``\$n` `+ 1; ``\$i``++)``    ``{``         ` `        ``// For power function, ``        ``// both its parameters ``        ``// are to be in double``        ``\$val` `= pow(``\$x``, ``\$i``);``         ` `        ``// calling countDigits ``        ``// function on x^i``        ``countDigits(``\$val``, ``\$freq_count``);``    ``} ``    ``// Printing count of digits 0-9``    ``for` `(``\$i` `= 0; ``\$i` `< 10; ``\$i``++)``    ``{``        ``echo` `\$freq_count``[``\$i``] . ``" "``;``}``}`` ` `// Driver code``\$x` `= 15;``\$n` `= 3;``countFrequency(``\$x``, ``\$n``)`` ` `// This code is contributed by mits``?>`
Output:
```0 1 2 2 0 3 0 1 0 0
```

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up