FastPrepCount Divisible Power Sums

Count Divisible Power Sums

GoodScore logoGoodScore● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

For every exponent triple (x, y, z) with 0 <= x <= maxExponent2, 0 <= y <= maxExponent3, and 0 <= z <= maxExponent5, form 2^x + 3^y + 5^z.

Return the number of exponent triples whose formed number is divisible by divisor.

Different exponent triples are counted separately, even if they produce the same numeric sum.

Implement countDivisiblePowerSums with integer parameters maxExponent2, maxExponent3, maxExponent5, and divisor. Return the number of valid triples as a long.

Function

countDivisiblePowerSums(maxExponent2: int, maxExponent3: int, maxExponent5: int, divisor: int) → long

Examples

Example 1

maxExponent2 = 0maxExponent3 = 0maxExponent5 = 0divisor = 3return = 1

The only triple is (0, 0, 0), producing 1 + 1 + 1 = 3, which is divisible by 3.

Example 2

maxExponent2 = 1maxExponent3 = 1maxExponent5 = 0divisor = 2return = 2

The triples (1, 0, 0) and (1, 1, 0) produce 4 and 6. The other two sums are odd.

Example 3

maxExponent2 = 2maxExponent3 = 2maxExponent5 = 2divisor = 5return = 7

Seven of the 27 exponent triples have a power sum congruent to 0 modulo 5.

Constraints

  • 0 <= maxExponent2, maxExponent3, maxExponent5 <= 2000
  • 1 <= divisor <= 2000
  • The answer fits in a signed 64-bit integer.

More GoodScore problems

See GoodScore hiring insights
public long countDivisiblePowerSums(int maxExponent2, int maxExponent3, int maxExponent5, int divisor) {
  // write your code here
}
maxExponent20
maxExponent30
maxExponent50
divisor3
expected1
Checking account…