Find Square Root of a Number (sqrt(x)) | Binary Search | Programming Tutorials

preview_player
Показать описание
In this tutorial, I have explained how to find square root of a number using binary search in Java.

Given an integer x (non-negative integer). Write a code to compute the square root of x.

If x is not a perfect square in that case return floor of square root of x (floor(sqrt(x))).

NOTE : Do not use sqrt function from standard library.

In this tutorial, I have discussed how to calculate square root of integer.

For example -

Example 1:

Input : 4

Output: 2


Example 2:

Input : 11

Output: 3

Square root of 11 is 3.316624 but we have return the integer part only
after truncating the decimal digits.

floor(3.316624) is 3


Example 3:

Input : 17

Output : 4

Square root of 17 is 4.12310. Floor(4.12310) is 4.

Рекомендации по теме
Комментарии
Автор

make a video on:- Rearrange the array in alternating positive and negative items...

arunyoutube
Автор

Hey bro, you are doing a great job and helping so many peoples, keep it up, i wish you the best, keep spreading knowledge 🙏,
You can leave your social account link below so that we can follow you there as well 🙏

Movieaddictionworld
Автор

This solution does not work for big integers. You need to do division instead of multiplication.
Instead of doing `if(mid * mid == x)`, do `if(x/mid == mid)`. Same for the rest of conditions.

fanifyeyev
Автор

why not use binary search for calculation.

kunal
Автор

Bro i dont know why i am getting time limit exceeded in leet code & perfectly executing in java for the same code in the link

surajgrandhi
welcome to shbcf.ru