DEV Community

Discussion on: LeetCode 69. Sqrt(x) (javascript solution)

Collapse
 
maparr profile image
Maksym Parfenov
if(mid*mid === x) return mid
Enter fullscreen mode Exit fullscreen mode

the task says, we need
Input: x = 4 // input
Output: 2 // we have to return

else if(mid*mid >x) right = mid
Enter fullscreen mode Exit fullscreen mode

if the mid * mid ( target ) is bigger we're going left side of the binary search ( in the case with numbers, not arrays, we're doing like this, if mid before was 4 , it is our input in first iteration, new value will be 2;

else left = mid+1
Enter fullscreen mode Exit fullscreen mode

says, we going to the right side of the binary search, and 2 + 1 = 3 and we are going to search again through while

Collapse
 
vishal590 profile image
vishal

thanks for reply