Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Square Root of A Number

Medium Acceptance 63.00% Points 30.00

Given a non-negative integer x, return the integer square root of x. The integer square root is the greatest integer y such that y × y = x. You must not use any built-in exponent or square root functions.

Examples
Example 1

Example 1

Input: x = 4

Output: 2

Explanation: 2 × 2 = 4, so the integer square root is 2.

Example 2

Example 2

Input: x = 8

Output: 2

Explanation: The square root of 8 is approximately 2.828, so the integer square root is 2.

Example 3

Example 3

Input: x = 0

Output: 0

Explanation: The square root of 0 is 0.

Hints
Hint 1
N/A
Constraints
  • 0 = x = 2³¹ - 1
Companies
Walmart
Topics
Binary Search
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions