Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Power Function

Recursion & Backtrackingeasy

Implement `power(x, n)` returning `x` raised to the non-negative integer `n`, using fast exponentiation.

Sample tests

Input: power(2, 10)
Output: 1024
Input: power(3, 0)
Output: 1

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Linear multiplication is O(n); fast exponentiation is O(log n).

Learning resources

  • Wikipedia: Backtracking
Approach & explanation (try first)

Exponentiation by squaring runs in O(log n) time.

Loading...
⌘/Ctrl + Enter

Run your code to see results.