FastPrepCount Powers of K (MLE)
Problems/Count Powers of K (MLE)

Count Powers of K (MLE)

Quora logoQuora● EasyINTERNOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

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 check
  • int k: the base

The function returns an int: how many elements of arr are powers of k.

Function

countPowersOfK(arr: int[], k: int) → int

Examples

Example 1

arr = [11, 121, 10]k = 11return = 2

11 = 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 ≤ 105
  • 1 ≤ arr[i] ≤ 109
  • 2 ≤ k ≤ 109

More Quora interview questions

  • Determine the Elimination Order (MLE)OA · Seen Dec 2024

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
All Quora interview questionsSee Quora hiring insights
Saved
class Solution {
    public int countPowersOfK(int[] arr, int k) {
      // write your code here
    }

}
Checking account…