Himanshi Verma’s Post

View profile for Himanshi Verma, graphic

Engineer - UPI @ Juspay | Frontend developer | React enthusiast | DevFolio UltraHacx Hackathon Runner-up | Leetcode (1598) | AFWWA Declamation Contest Winner | LPU'24

🌟 Day 91 of #100DaysOfLeetCode Challenge! 🚀 Today I solved the problem no. 50 on LeetCode: Pow(x, n) APPROACH : 1. If n == 0: Any number raised to the power of 0 is 1, so return 1. 2. If x == 0: Any non-zero number raised to the power of 0 is 1, so return 0. 3. If n < 0: We need to compute the reciprocal of x raised to the positive value of n (-n). This is handled by setting x = 1/x and making n positive (n = -n). 4. Initialize ans to 1, which will accumulate the result of x raised to the power as we iterate. 5. Use a while loop that continues as long as n is non-zero. 6. Inside the loop: If n is even (n % 2 == 0), square x (x = x * x) and halve n (n = n / 2). If n is odd (n % 2 == 1), multiply ans by x (ans = ans * x) and decrement n by 1 (n = n - 1). 7. Once the while loop completes (i.e., n becomes zero), ans contains x raised to the power of n, considering the adjustments made for negative exponents (if any). 8. Return ans, which is the computed result of x raised to the power n. TIME COMPLEXITY: O(logn) SPACE COMPLEXITY: O(1) Feel free to contribute your approaches as well. #100DaysOfLeetCode #CodingChallenge #Algorithms #Programming  #TechSkills #CodingAdventure #GeekModeActivated  #leetcodechallenge

  • No alternative text description for this image

To view or add a comment, sign in

Explore topics