BS-10. Finding Sqrt of a number using Binary Search

preview_player
Показать описание


Find DSA, LLD, OOPs, Core Subjects, 1000+ Premium Questions company wise, Aptitude, SQL, AI doubt support and many other features that will help you to stay focussed inside one platform under one affordable subscription. Have a hassle free one stop solution for up-skilling and preparing.

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

Hi, hope you are well.


Find DSA, LLD, OOPs, Core Subjects, 1000+ Premium Questions company wise, Aptitude, SQL and many other time saving features under one affordable subscription. Have a hassle free one stop solution for up-skilling and preparing yourself.


takeUforward
Автор

The check "mid * mid <= x" may lead to integer overflow. instead, by dividing mid on both sides, we can write "mid <= x / mid"

varun
Автор

Had been desperately waiting for the series to restart. Thanks a lot.

piyushraj
Автор

i never thought this could be the solution for finding sqrt ....mind blowing

KunalSingh-mf
Автор

Understood!!
Here are the time stamps:
00:00 - 00:15 Intro
00:16 - 1:15 -Problem description
1:16 - 4:00 - Brute(linear)
4:01-5:20 - Binary search intuition
5:20 - 16:16 - Optimal solution
16:17 - 16:45- code
16:45 - 17:10 - outro

suyashmishra
Автор

Hats Off to the content and it's creator ! Understood each and every part of the video.

Dipanshutripathi
Автор

Correction at time 12:45 the time complexity of the brute force solution is not O(n) as suggested by striver but it is O(sqrt n) according to me as the loop will break after that condition always

And plz striver bhaiya vidios jaldi jaldi daal do bhot wait kar liya

mayankshakya
Автор

You can also take high as n/2 rather than n intially as it is certain that ans will lie in a range of 1 to n/2

jasrajsehmbey
Автор

Excellent explanation ❣.. Waiting for linked list series💛💛

adarshkumarrao
Автор

Well explained!! I finally understand why the answer converges to "right" especially from 12:00

jfzodgb
Автор

dada, cant we optimize it further by reducing the search space. we can keep high as n//2 initially cause a square root of a number can never be greater than half of the number.

pushankarmakar
Автор

Good to see from brute force approach to optimal approach.

Manishgupta
Автор

I aam doing dsa from tha last 6 months and i found u yesterday and now i am a fannnn...

sohamchatterjee
Автор

first naive approach had TC of O(sqrt(n)) but binary search has TC of O(log(n)) which is eventually slightly better than former one for very large values.

iWontFakeIt
Автор

int mySqrt(int n) {
if(n==0)return 0;if(n==1)return 1;
int ans=-1;
int start=1;int end=n/2;
while(start<=end){
long mid=start+(end-start)/2;
if(mid*mid<=n){
ans=mid;
start=mid+1;
}else{
end=mid-1;
}
}
return ans;

}

mihirkumar
Автор

Bhaiya why can't we use high as n/2 instead of n at start in the function(instead of 28 we can use 14 ) 13:14

kiransoorya
Автор

Hi Raj,

I think the solution can be bit more efficient with the TC of O(log n/2) instead of O(log n). Since, we know square root of any number n can't be bigger than half of that number i.e, n/2 therefore we would take high as n/2 instead of n. Here's the code:

class Solution {
public int mySqrt(int x) {

if(x == 1)
return 1;

int low = 1, high = x/2;
int result = 0;

while(low <= high) {
int mid = low + (high-low)/2;

if((long)mid*mid <= x) {
result = mid;
low = mid+1;
} else
high = mid-1;
}
return result;
}
}

impalash_ag
Автор

we can further optimise this by taking range of only x//2 because the square root number will be smaler than that except for x = 1 that case can be covered separately

Anushka
Автор

amazing explanation striver, understood.

SuvradipDasPhotographyOfficial
Автор

Respect to Striver ❤ for such a easy explanation

subhankarkanrar
visit shbcf.ru