Problem List

Sqrt(x)

May 31, 2025Go math, binary searcheasy

Problem

Reflections

The binary search topic tag kind of gave it away.

Performance

Complexity

Go Solution
func mySqrt(x int) int {
  left, right := 1, x

  for left <= right {
    mid:= left + ((right - left) / 2)

        mid2:= mid * mid

        if mid2 < x {
      left = mid + 1
    } else if mid2 > x {
      right = mid - 1
    } else {
    return mid
  }
}

return right
}
LeetCode Problem Link