Math & Geometry/Medium

Pow(x, n)PRO PASS

Time: O(log N)Space: O(1)

Step 1: Setup & Initialization - Pow(x, n)

Binary exponentiation: square base x and halve exponent n at each step.

Array Elements & Pointers
10
[0]
20
[1]
30
[2]
40
[3]
50
[4]
Live Variables & Invariants
status:Initialized
pattern:Pow(x, n)
Step 1 / 333%
Solution Code
1
function myPow(x: number, n: number): number {
2
  if (n === 0) return 1;
3
  if (n < 0) { x = 1 / x; n = -n; }
4
  let res = 1;
5
  while (n > 0) {
6
    if (n % 2 === 1) res *= x;
7
    x *= x; n = Math.floor(n / 2);
8
  }
9
  return res;
10
}

Custom Test Case Runner

Input your custom values and visualize step-by-step trace

Quick Presets:

AI DSA Coach

Contextual Tutor for Pow(x, n)

Hello! I am your AI DSA Tutor for **Pow(x, n)** (Math & Geometry). Ask me anything about this algorithm, time complexity, or request a step hint!