Back to photostream

Sqrt(x) Leetcode Solution

As the title says, we need to find the square root of a number. Let say the number is x, then Sqrt(x) is a number such that Sqrt(x) * Sqrt(x) = x. If the square root of a number is some decimal value, then we have to return the floor value of the square root.

Example

4

2

7

2

Approach(Pre-built functions)

The math library of C++ and lang.Math library of Java have the pre-built functions to return the square root of a number. We can apply floor() to avoid any decimal value.

Algorithm

 

- If the number is less than 2, return itself

- Call the sqrt() function

- Floor the value obtained

- Print the result

 

Implementation of Sqrt(x) Leetcode Solution

C++ Program

#include

using namespace std;

 

int mySqrt(int x)

{

if(x

 

www.tutorialcup.com/leetcode-solutions/sqrtx-leetcode-sol...

11 views
0 faves
0 comments
Uploaded on October 21, 2021