Computing Sqrt(x) with Binary Search on LeetCode

🎉 Day 8 of My 100-Day Programming Challenge! 🚀 Today, I solved the “Sqrt(x)” problem on LeetCode. The task is to compute the square root of a non-negative integer and return the result rounded down to the nearest integer, without using built-in functions like pow() or sqrt(). 🧩 🔍 Approach: I used the Binary Search technique to efficiently find the square root. ⚙️ Steps: • Define the search space from 1 to x. • Calculate the middle value (mid). • Check if mid * mid is equal to x. • If less than x, move right and store the potential answer. • If greater than x, move left. • Continue until the search space is exhausted. 💡 Key Insights: ✔ Binary Search reduces time complexity significantly. ✔ Avoids overflow using mid <= x / mid instead of mid * mid. ✔ Efficient way to compute roots without built-in functions. 🧠 Complexity: • Time Complexity: O(log n) • Space Complexity: O(1) Another great problem to strengthen Binary Search fundamentals and mathematical problem-solving skills. On to the next challenge! 💻🔥 #100DaysOfCode #Java #LeetCode #DSA #BinarySearch #Programming #CodingChallenge

  • text

To view or add a comment, sign in

Explore content categories