# Leetcode 69 - Sqrt

Given a non-negative integer `x`

, compute and return *the square root of* `x`

.

Since the return type is an integer, the decimal digits are **truncated**, and only **the integer part** of the result is returned.

**Example 1:**

1 | Input: x = 4 |

**Example 2:**

1 | Input: x = 8 |

**Constraints:**

`0 <= x <= 231 - 1`

**Thoughts**: To use `binary search`

, initialize `left`

as `0`

and `right`

as `Number.MAX_VALUE`

. Everytime we compare `mid`

with `x/mid`

in which `mid`

is just `left + (right-left)/2`

. We have to write it in this form so it wonâ€™t overflow. Also, when we compare if `mid^2`

is bigger or smaller than `x`

, we have to write it as `mid`

vs `x/mid`

.

1 | /** |