LeetCode Problem Workspace

Super Pow

Compute large exponentiations efficiently using modular arithmetic and divide-and-conquer techniques for this Super Pow problem.

category

2

Topics

code_blocks

5

Code langs

hub

3

Related

Practice Focus

Medium · Math plus Divide and Conquer

bolt

Answer-first summary

Compute large exponentiations efficiently using modular arithmetic and divide-and-conquer techniques for this Super Pow problem.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Math plus Divide and Conquer

Try AiBox Copilotarrow_forward

To solve Super Pow, directly computing a^b is infeasible due to b's potential length. We apply modular exponentiation with divide-and-conquer, breaking the problem into smaller powers while taking modulo 1337 at each step. This approach ensures calculations stay manageable and leverages both the numeric pattern and array-based exponent structure efficiently.

Problem Statement

Given a positive integer a and a very large positive integer b represented as an array of digits, compute a^b mod 1337. The array b contains digits in order from most to least significant, and direct exponentiation may overflow standard numeric types.

For example, a = 2 and b = [3] yields 2^3 = 8, while a = 2 and b = [1,0] yields 2^10 = 1024. Your task is to implement an efficient solution that handles extremely large arrays b while respecting modular arithmetic constraints.

Examples

Example 1

Input: a = 2, b = [3]

Output: 8

Example details omitted.

Example 2

Input: a = 2, b = [1,0]

Output: 1024

Example details omitted.

Example 3

Input: a = 1, b = [4,3,3,8,5,2]

Output: 1

Example details omitted.

Constraints

  • 1 <= a <= 231 - 1
  • 1 <= b.length <= 2000
  • 0 <= b[i] <= 9
  • b does not contain leading zeros.

Solution Approach

Modular Exponentiation with Recursion

Break the array b into its last digit and remaining prefix. Compute a^(prefix*10) and a^last_digit separately, applying modulo 1337 at each step. Recursively combine results to obtain the final modular power efficiently.

Optimize with Divide-and-Conquer

Use divide-and-conquer to reduce the exponentiation problem: split b recursively to compute smaller powers. Multiply partial results modulo 1337 to prevent overflow, maintaining O(log n) recursive depth for speed.

Handle Edge Cases Carefully

Account for a = 1 which always returns 1 regardless of b. Ensure leading zeros in b are ignored, and confirm the implementation correctly reduces powers modulo 1337 at every recursion to avoid numeric overflows.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity depends on the length of b; each recursive step reduces b by one digit, and modular exponentiation takes constant time per step, yielding roughly O(n log a) time. Space complexity is O(n) due to recursion stack proportional to b's length.

What Interviewers Usually Probe

  • Expect you to recognize that direct exponentiation will overflow for large b arrays.
  • Interviewers want to see divide-and-conquer applied to array-based exponentiation with modulo optimization.
  • Clarifying edge cases for a = 1 or b = [0] can demonstrate thorough understanding.

Common Pitfalls or Variants

Common pitfalls

  • Attempting direct calculation of a^b without modular reduction causes overflow.
  • Ignoring the array nature of b leads to miscalculating large exponents.
  • Forgetting to apply modulo 1337 at each step produces incorrect results.

Follow-up variants

  • Compute a^b mod m for different modulus values, adjusting recursive multiplication accordingly.
  • Handle exponents given in string format rather than array digits.
  • Support negative base values with proper modular handling.

FAQ

What is the most efficient way to compute Super Pow for very large b arrays?

Use modular exponentiation with divide-and-conquer, recursively splitting b and applying modulo 1337 at each step.

Why can't I directly compute a^b in Super Pow?

Because b can be extremely large, direct computation will overflow typical integer types and is impractical.

How do I handle b represented as an array of digits?

Split b into prefix and last digit recursively, compute each partial power modulo 1337, then combine results.

What edge cases should I consider in Super Pow?

Check for a = 1, b = [0], or any leading zeros in b, ensuring all modular steps are applied correctly.

Can GhostInterview show intermediate steps for debugging Super Pow?

Yes, it breaks down recursive calls and modular multiplications, helping trace how final results are computed safely.

terminal

Solution

Solution 1

#### Python3

1
2
3
4
5
6
7
8
class Solution:
    def superPow(self, a: int, b: List[int]) -> int:
        mod = 1337
        ans = 1
        for e in b[::-1]:
            ans = ans * pow(a, e, mod) % mod
            a = pow(a, 10, mod)
        return ans
Super Pow Solution: Math plus Divide and Conquer | LeetCode #372 Medium