Count Powers of K (MLE)
Updated
Problem statement
Given an array arr and a value k, find the count of numbers in the array that are powers of k. For example, if arr = [11, 121, 10] and k = 11, the result would be 2.
A number is a power of k if it equals kp for some whole number p ≥ 0. So 1 = k0 counts, and so do k, k2, k3, and so on. If a number appears several times in arr, count every appearance.
Complete the function countPowersOfK in the editor.
countPowersOfK has the following parameters:
int[] arr: the numbers to checkint k: the base
The function returns an int: how many elements of arr are powers of k.
Function
countPowersOfK(arr: int[], k: int) → intExamples
Example 1
arr = [11, 121, 10]k = 11return = 211 = 111 and 121 = 112 are powers of 11. 10 is not, because 110 = 1 and 111 = 11 already skip past it. So the count is 2.
Constraints
1 ≤ arr.length ≤ 1051 ≤ arr[i] ≤ 1092 ≤ k ≤ 109
More Quora interview questions
Similar questions at other companies
- Count Valid Square PairsAuxoAI · OA
- Picked Player Who LostSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Server InvestmentRippling · OARippling interview questions
- Basic Item DiscountsPalantir · OAPalantir interview questions
- Next Pascal Row CoefficientIntuit · OAIntuit interview questions
- Unique Integers That Sum to ZeroNVIDIA · OANVIDIA interview questions
- Count Multiples of Three with Two SevensHudson River Trading · OAHudson River Trading interview questions
- Diagonal Bounce TraversalAutodesk · OAAutodesk interview questions
- Maximum Student-Teacher DistanceChubb · OAChubb interview questions
- Maximum Three-Server Difficulty ScoreVisa · OAVisa interview questions