QuoraOA #1
Count Powers of K (MLE)
EasyJudge-ready
Topics (AI-labelled)Bits & Math
Given an integer array arr and an integer k, count how many numbers in the array are powers of k, that is, numbers that can be written as k^e for a positive integer e.
Input
The first line contains two integers n and k; the second line contains n integers arr[0..n−1]. 1≤n≤200000, 2≤k≤10^18, 2≤arr[i]≤10^18.
Output
Output one integer: the number of elements that are powers of k.
Example 1
Input:
3 11
11 121 10
Output:
2
Example 2
Input:
5 2
2 4 6 8 1024
Output:
4
Example 3
Input:
3 5
10 20 30
Output:
0
Example notes
Example 1: arr=[11,121,10], k=11. 11=11^1 and 121=11^2, but 10 is not, so the answer is 2. Example 2: 2, 4, 8, and 1024 are all powers of 2, but 6 is not, so the answer is 4. Example 3: none of 10, 20, 30 is a power of 5, so the answer is 0.
Editorial
Loading solution…