Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.
Example 1:
Input: a = 2, b = [3] Output: 8
Example 2:
Input: a = 2, b = [1,0] Output: 1024
Example 3:
Input: a = 1, b = [4,3,3,8,5,2] Output: 1
Constraints:
1 <= a <= 231 - 11 <= b.length <= 20000 <= b[i] <= 9b does not contain leading zeros.Problem summary: Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
2 [3]
2 [1,0]
1 [4,3,3,8,5,2]
powx-n)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #372: Super Pow
class Solution {
private final int mod = 1337;
public int superPow(int a, int[] b) {
long ans = 1;
for (int i = b.length - 1; i >= 0; --i) {
ans = ans * qpow(a, b[i]) % mod;
a = qpow(a, 10);
}
return (int) ans;
}
private long qpow(long a, int n) {
long ans = 1;
for (; n > 0; n >>= 1) {
if ((n & 1) == 1) {
ans = ans * a % mod;
}
a = a * a % mod;
}
return ans;
}
}
// Accepted solution for LeetCode #372: Super Pow
func superPow(a int, b []int) int {
const mod int = 1337
ans := 1
qpow := func(a, n int) int {
ans := 1
for ; n > 0; n >>= 1 {
if n&1 == 1 {
ans = ans * a % mod
}
a = a * a % mod
}
return ans
}
for i := len(b) - 1; i >= 0; i-- {
ans = ans * qpow(a, b[i]) % mod
a = qpow(a, 10)
}
return ans
}
# Accepted solution for LeetCode #372: Super Pow
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
// Accepted solution for LeetCode #372: Super Pow
struct Solution;
impl Solution {
fn super_pow(a: i32, mut b: Vec<i32>) -> i32 {
let a = a % 1337;
if let Some(last) = b.pop() {
Self::pow_mod(Self::super_pow(a, b) % 1337, 10) * Self::pow_mod(a, last) % 1337
} else {
1
}
}
fn pow_mod(a: i32, k: i32) -> i32 {
let mut res = 1;
for _ in 0..k {
res *= a;
res %= 1337;
}
res
}
}
#[test]
fn test() {
let a = 2;
let b = vec![3];
let res = 8;
assert_eq!(Solution::super_pow(a, b), res);
let a = 2;
let b = vec![1, 0];
let res = 1024;
assert_eq!(Solution::super_pow(a, b), res);
}
// Accepted solution for LeetCode #372: Super Pow
function superPow(a: number, b: number[]): number {
let ans = 1;
const mod = 1337;
const qpow = (a: number, n: number): number => {
let ans = 1;
for (; n; n >>= 1) {
if (n & 1) {
ans = Number((BigInt(ans) * BigInt(a)) % BigInt(mod));
}
a = Number((BigInt(a) * BigInt(a)) % BigInt(mod));
}
return ans;
};
for (let i = b.length - 1; ~i; --i) {
ans = Number((BigInt(ans) * BigInt(qpow(a, b[i]))) % BigInt(mod));
a = qpow(a, 10);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.