Implement pow(x, n) using Fast Exponentiation

🧩 Day 37 — Pow(x, n) (LeetCode 50) 📝 Problem Implement pow(x, n), which calculates x raised to the power n (i.e., xⁿ). 🔁 Approach -Use Fast Exponentiation (Binary Exponentiation) to compute the result efficiently. -If n is negative, compute the reciprocal: → xⁿ = 1 / (x⁻ⁿ) -When n is even → xⁿ = (x²)^(n/2) -When n is odd → xⁿ = x * xⁿ⁻¹ -This method reduces the number of multiplications drastically. -Implement iteratively for better space efficiency (no recursion stack). 📊 Complexity -Time Complexity : O(log n) -Space Complexity : O(1) 🔑 Concepts Practiced -Exponentiation by squaring -Bit manipulation logic (n //= 2) -Handling negative powers -Mathematical optimization #Leetcode #Python #DSA #Math #Power

  • No alternative text description for this image

This question from striver sde sheet keep doing 🙂

To view or add a comment, sign in

Explore content categories