Java
Java copied to clipboard
Optimize power() using exponentiation by squaring (O(log n))
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.