Java icon indicating copy to clipboard operation
Java copied to clipboard

Optimize power() using exponentiation by squaring (O(log n))

Open Vinayak-v12 opened this issue 3 weeks ago • 2 comments

This PR optimizes the power() method by replacing the linear recursive implementation with an exponentiation-by-squaring approach.

✔ Time complexity reduced from O(n) → O(log n) ✔ Handles negative exponents ✔ Keeps same behavior as original implementation ✔ Fully compatible with existing test cases ✔ No API changes

This improves performance and reduces recursion depth while maintaining clean readability.

Vinayak-v12 avatar Nov 15 '25 07:11 Vinayak-v12