← Divide & Conquer

Pattern Recognition Drill

#121 — Pow(x, n)

Medium Math & Geometry

The Problem

Implement pow(x, n) — compute x raised to the power n.

What approach would you use?

Think about it before scrolling down.

Key Signals

Divide & Conquer

x^n = (x^(n/2))². If n is odd, multiply by x. O(log n).

Alt: Math / Number Theory

Iterative: square x and halve n, accumulating when n is odd. Same O(log n).

Common Trap

Handle n < 0 by computing 1/x^(-n). Watch for integer overflow when negating n.

← #120