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

  Рет қаралды 144,823

take U forward

take U forward

Күн бұрын

Пікірлер: 241
@varun1017
@varun1017 9 ай бұрын
The check "mid * mid
@anigaming7146
@anigaming7146 9 ай бұрын
Thanks bro😊
@varun1017
@varun1017 9 ай бұрын
@@anigaming7146 welcome
@pushankarmakar1783
@pushankarmakar1783 9 ай бұрын
thanks brother, i was also thinking of the same issue but since there were no errors in the testcases so i ignored it. they can bring up this issue in an interview
@varun1017
@varun1017 9 ай бұрын
@@pushankarmakar1783 all the best
@harshitrautela6585
@harshitrautela6585 8 ай бұрын
Thanks for help.❤❤
@piyushraj5464
@piyushraj5464 Жыл бұрын
Had been desperately waiting for the series to restart. Thanks a lot.
@jasrajsehmbey
@jasrajsehmbey Жыл бұрын
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
@takeUforward
@takeUforward Жыл бұрын
Yes
@uditgarg6508
@uditgarg6508 5 ай бұрын
i did the same initially
@gamer_bhai259
@gamer_bhai259 2 ай бұрын
for 1 all write differently ri8>\]
@mihirkumar4770
@mihirkumar4770 8 ай бұрын
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
@mayankshakya9200
@mayankshakya9200 Жыл бұрын
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
@champion5946
@champion5946 Жыл бұрын
thodi sharam kr... wo banda full time job me itna kr rha h... patience nhi h logo ko free ki chizz bhi jalid chaiye
@mayankshakya9200
@mayankshakya9200 Жыл бұрын
@@champion5946 chal na lavde aa thuu maine mana kiya kya ki wo efforts nahi maar raha striver ke liye 1000 percent izzat hai mere dil me and mere msg ka wo matlab nahi tha
@kumarshivam3661
@kumarshivam3661 Жыл бұрын
​@@champion5946are didi placement hai next month se isliye thoda patience nhi rakh paa rhe log
@adarshkumarrao3478
@adarshkumarrao3478 Жыл бұрын
Excellent explanation ❣.. Waiting for linked list series💛💛
@indroneelgoswami5654
@indroneelgoswami5654 3 ай бұрын
I solved this question without watching the video!!! OMG!!! STRIVER YOU BEAUTY!!!!!!!!!!!!!!!
@iWontFakeIt
@iWontFakeIt Жыл бұрын
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.
@suyashmishra6946
@suyashmishra6946 Жыл бұрын
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
@Shanky_17
@Shanky_17 Жыл бұрын
Why bro? just why ??
@hoang640
@hoang640 Жыл бұрын
@@Shanky_17 Just love him for that
@souviksen7345
@souviksen7345 Жыл бұрын
Always top level concept clearer ❤
@Dipanshutripathi2407
@Dipanshutripathi2407 Жыл бұрын
Hats Off to the content and it's creator ! Understood each and every part of the video.
@Manishgupta200
@Manishgupta200 Жыл бұрын
Good to see from brute force approach to optimal approach.
@sohamchatterjee6934
@sohamchatterjee6934 Жыл бұрын
I aam doing dsa from tha last 6 months and i found u yesterday and now i am a fannnn...
@DR-mq1le
@DR-mq1le Жыл бұрын
hows your dsa prep going now , i just started , does it get any better?🤣
@sudhanshushekhar4222
@sudhanshushekhar4222 Жыл бұрын
Understood; but to be noted we can also take low = 1 and high = n/2 this also works because sqrt of a number is always less than or equal to n/2. But I guess here it do not matter as this will only reduce one iteration but if we are dealing with a program which regularly calculate the sqrt these one one iteration which is reduded might sum up and save a lot of time..[ just a random thought]
@abhimanyushekhawat2626
@abhimanyushekhawat2626 Жыл бұрын
What about 2?
@dhruvchopra26
@dhruvchopra26 11 ай бұрын
@@abhimanyushekhawat2626 Works for 2 as well.
@artifice_abhi
@artifice_abhi 10 ай бұрын
just right an if condition at the start @@abhimanyushekhawat2626
@vedikamishra009
@vedikamishra009 10 күн бұрын
@@abhimanyushekhawat2626 for n=2 the answer is 1, if you carefully observe low = 1 high = 1 and mid is also 1*1
@pushankarmakar1783
@pushankarmakar1783 9 ай бұрын
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.
@saireddyreddy8969
@saireddyreddy8969 9 ай бұрын
Your explanation is just awesome.
@thetechmasum
@thetechmasum Жыл бұрын
using the concept of lower bound : private static int getSquareRoot(int n) { int low = 1; int high = n; while (low(long)(n)){ high =(int) mid-1; } else { low =(int) mid +1 ; } } return low-1; }
@lucario404
@lucario404 Жыл бұрын
we can check sqrt from 1(low) to n/2 (high) as well
@SuvradipDasPhotographyOfficial
@SuvradipDasPhotographyOfficial Жыл бұрын
amazing explanation striver, understood.
@subhankarkanrar9494
@subhankarkanrar9494 9 ай бұрын
Respect to Striver ❤ for such a easy explanation
@KarthikNandam-xs4qn
@KarthikNandam-xs4qn Ай бұрын
use the check value as if( mid
@oyeesharme
@oyeesharme 3 ай бұрын
thanks bhaiya for your endless efforts
@impalash_ag
@impalash_ag 4 ай бұрын
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
@Josuke217
@Josuke217 4 ай бұрын
@@impalash_ag good observation
@himanshukamble765
@himanshukamble765 4 ай бұрын
yup i also thought of that
@higgsboson67
@higgsboson67 3 ай бұрын
​@@himanshukamble765 what about n=2 ?
@thanos8323
@thanos8323 2 ай бұрын
@@higgsboson67 for n=2 the answer is 1, if you carefully observe low = 1 high = 1 and mid is also 1*1
@manavsingh5919
@manavsingh5919 Жыл бұрын
Thank you Striver...Understood everything🙂
@AtulKumar-c4x7l
@AtulKumar-c4x7l Жыл бұрын
understood.. Thank you striver for such an amazing explanation
@AbhiSharma-ku7dq
@AbhiSharma-ku7dq 4 ай бұрын
Thank you for creating such courses
@ashishpradhan6250
@ashishpradhan6250 5 ай бұрын
understood clearly...thanks a lot, u have given me hope!
@tripd4949
@tripd4949 6 ай бұрын
Wow that was a very slick approach, good job.
@VishalGupta-xw2rp
@VishalGupta-xw2rp Жыл бұрын
If you observe then we don't really have to take high = n Because in all the cases we are only going till half of n.... Sooooo I think we can further reduce our search space 1 to n/2 and then apply binary search 👨‍💻
@muskan_bagrecha
@muskan_bagrecha 10 ай бұрын
Right. Although even if you start with n, the first partition will end up dividing it into half. So should not make a huge difference. Also if you are considering n/2, we need to make sure we are not rounding down values like 1.
@Manasidas99
@Manasidas99 Жыл бұрын
Great video you made thank you very much sir waiting for the linked list Video.
@cinime
@cinime Жыл бұрын
Understood! Awesome explanation as always, thank you very very much for your effort!!
@parulvats
@parulvats Жыл бұрын
In pseudo code it was mentioned either we can return ans or high, but returning ans giving error
@hareshnayak7302
@hareshnayak7302 7 ай бұрын
Understood, Thanks striver for this amazing video.
@SatyaPrakash-dj8ix
@SatyaPrakash-dj8ix 6 ай бұрын
this is somewhat similar to Newton-Raphson method we use in engineering mathematics.
@shreyanshkumar4528
@shreyanshkumar4528 Жыл бұрын
Just awesome explanation.
@VishalGupta-xw2rp
@VishalGupta-xw2rp Жыл бұрын
10:50 Opposite Polarity Lec 12, 13 also has
@graviton001
@graviton001 3 ай бұрын
Was able to do it myself thnx ❤
@umairislam505
@umairislam505 28 күн бұрын
You are awesome brother
@mcbotface
@mcbotface Жыл бұрын
Largest integer or smallest integer are correct terminologies.
@sushantguria8820
@sushantguria8820 3 ай бұрын
Understood 🤘
@srilathareddy9450
@srilathareddy9450 Жыл бұрын
Superb explanation
@biryanibroseph210
@biryanibroseph210 7 ай бұрын
awesome explanation sir
@lshuarckyma
@lshuarckyma Ай бұрын
STRIVERS TIP = if one part of a vector / data strc can be fully eliminated from consideration then binary search can be used
@manusklm1161
@manusklm1161 3 ай бұрын
checking if the (mid*mid==x) and returning mid before the actual "check" makes the code more efficient . am i right?
@Josuke217
@Josuke217 4 ай бұрын
Binary Search on answers finally !
@DeepakPatel-d5v
@DeepakPatel-d5v 7 ай бұрын
Thanks a lot Bhaiya
@avisoft-l2p
@avisoft-l2p 3 ай бұрын
if we take high as n/2, then also we will get a desirable ans! US bhaiya
@RaunitJaiswal-s9v
@RaunitJaiswal-s9v 2 ай бұрын
Started 2 video karke sojaynege bhaya 😮😮
@kiransoorya7895
@kiransoorya7895 5 ай бұрын
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
@AnmolGupta-oj4lm
@AnmolGupta-oj4lm Жыл бұрын
Understood Very Well!
@ishasingh6726
@ishasingh6726 Жыл бұрын
Thank youuu striver❤
@sukhpreetsingh5200
@sukhpreetsingh5200 Жыл бұрын
❤Awesome as usual
@Spavan3011
@Spavan3011 6 ай бұрын
What if we have to find exact ans not an integer?
@kumarshivam3661
@kumarshivam3661 Жыл бұрын
Striver Bhaiya please august end tak a to z sheet complete kra do
@EC20022ELAKKIYAC
@EC20022ELAKKIYAC 10 ай бұрын
Understood!!! Thank You
@NazeerBashaShaik
@NazeerBashaShaik 7 ай бұрын
Understood, thank you.
@utsavseth6573
@utsavseth6573 Жыл бұрын
Understood RAJ.
@amanasrani6405
@amanasrani6405 4 ай бұрын
Understood Bhaiya
@keshavbiyani9202
@keshavbiyani9202 5 ай бұрын
can someone explain how is linear O(N) ?? It will always be O(sqrt(N)) only right ?? Because for 28 we at max take 6 iterations.
@livinkumarsaravanan3781
@livinkumarsaravanan3781 4 ай бұрын
Worth content 👍
@sparrow_harsh
@sparrow_harsh Жыл бұрын
Can you come up with a video where we need to find square root upto decimal precision p
@Aditi-do4im
@Aditi-do4im 11 ай бұрын
you r incredible !!!!!!!!!!
@PrioritizingPeace
@PrioritizingPeace 3 ай бұрын
27 July 2024 Saturday 10:42 PM done ✅
@pradipkumarmukhi
@pradipkumarmukhi Ай бұрын
Understood 😇
@YourCodeVerse
@YourCodeVerse 11 ай бұрын
Understood✅🔥🔥
@UddhikaIshara
@UddhikaIshara Жыл бұрын
Thank you very much
@DeadPoolx1712
@DeadPoolx1712 3 ай бұрын
UNDERSTOOD;
@vigneshlion9309
@vigneshlion9309 11 ай бұрын
great video!❣
@syedFAHIM-el1wr
@syedFAHIM-el1wr Жыл бұрын
Thanks you a lot, Can't thank enough
@dhairyachauhan6622
@dhairyachauhan6622 Жыл бұрын
bhaiya please ek series bitmasking pe bhi bna dena. I know you already are busy.But jab ye playlist khatam ho jaye aur time mile then plese consider. If you have any recordings/ suggestions on how to learn toh please ek bar bta dena 😞😞😞
@rhul0017
@rhul0017 Жыл бұрын
if we find the sqrt of 9 then, i found that the high wont point to last valid integer, only when low crosses high,high points to 3, is this same for others?
@hrushi_borhade
@hrushi_borhade Жыл бұрын
Understood striver!!
@akbunofficial1281
@akbunofficial1281 Жыл бұрын
Hey!, can anyone tell , why this code is not working in one of the test cases of coding ninjas,(actually , i am unable to find that test cases on coding ninjas), but it is passing all test cases on gfg, if anyone can figure out?
@Simran-ns8gp
@Simran-ns8gp 11 ай бұрын
Check the data type..mine was also not wokring but then I changed the data types to long long , it passed all the test cases
@muskan_bagrecha
@muskan_bagrecha 10 ай бұрын
mine was failing for n = 1, as I had my upper bound at n/2 which was rounding it down to 0.
@Omi69-l4q
@Omi69-l4q 5 ай бұрын
@@Simran-ns8gp i also changed but it is not working pls tell
@SYCOA12CHAITANYAASOLE
@SYCOA12CHAITANYAASOLE 5 ай бұрын
Understood !! 😎😎
@kushagramishra5638
@kushagramishra5638 Жыл бұрын
understood!
@md.imrankhan6912
@md.imrankhan6912 Жыл бұрын
Legendary boss
@senseiAree
@senseiAree Жыл бұрын
Understood ❤
@arihantjammar8888
@arihantjammar8888 Жыл бұрын
UNDERSTOOD
@ambaradhikari7425
@ambaradhikari7425 Жыл бұрын
waiting for linked list, bit manipulation and strings please
@MohammedHasmi577
@MohammedHasmi577 7 ай бұрын
❤❤great video bro
@parassetia4964
@parassetia4964 4 ай бұрын
Understood boss^^
@sarangkumarsingh7901
@sarangkumarsingh7901 6 ай бұрын
Understood.......
@RaviKumar-sn6tu
@RaviKumar-sn6tu 7 ай бұрын
understood!!
@meghasharma4875
@meghasharma4875 2 ай бұрын
can i take low 1 and high n/2 beacuase square root of any no. never touch nth position
@navneetrai9512
@navneetrai9512 2 ай бұрын
yes, bcz squrt of any number can't be grater than or equal to 'number/2 '
@mehedihasansabbir7886
@mehedihasansabbir7886 Жыл бұрын
Superb
@raholsaha8191
@raholsaha8191 Жыл бұрын
great video
@rajashekar9912
@rajashekar9912 Жыл бұрын
Bhaiya plz start linked list series
@priyeshtandel2101
@priyeshtandel2101 Жыл бұрын
Understood
@DineshC-d8p
@DineshC-d8p Жыл бұрын
in this problem why we use binary search because we do in o(1) --> { ans=(n**0.5) ans=SquareRoot value } anyone explain why we go with binary search. Thanks
@lucifersamrat6280
@lucifersamrat6280 Жыл бұрын
just to test our problem solving skills
@HARSHRAJ-wz2rp
@HARSHRAJ-wz2rp 3 ай бұрын
The demand of Levis Tshirt increses from Tomorrow
@harshitjaiswal9439
@harshitjaiswal9439 Жыл бұрын
Understood!
@princepandey1836
@princepandey1836 9 ай бұрын
bhai ans return krene pr 1 test failed hora. but high pe all test cases passed. can someone tell me why?(coding ninjas). also int ans ; or int ans = 0; --> EVery test case passed but, int ans = -1 or 1. one test case failing why
@AbhishekBhattacharjee-j2m
@AbhishekBhattacharjee-j2m Жыл бұрын
UNDERSTOOD
@thedominatorscircle6762
@thedominatorscircle6762 Жыл бұрын
Bhi logic kayse build Karu kuch batao plzzzz
@crazymemes4080
@crazymemes4080 Жыл бұрын
Understood bhayaaaaaaaa
@roshangupta1686
@roshangupta1686 11 ай бұрын
underatood
@humanity7880
@humanity7880 Жыл бұрын
understood!
@astroop1528
@astroop1528 Жыл бұрын
Can someone tell me why my code is giving wrong answer. int floorSqrt(int n) { long long s = 1, e = n; long long ans = 1; while(s
@dreamyme543
@dreamyme543 Жыл бұрын
Understood🙃
@AmanKumar-qz4jz
@AmanKumar-qz4jz Жыл бұрын
thank u sir
BS-11. Find the Nth root of an Integer
20:44
take U forward
Рет қаралды 129 М.
Мама у нас строгая
00:20
VAVAN
Рет қаралды 10 МЛН
Median of 2 sorted arrays
26:38
Techdose
Рет қаралды 7 М.
BS-9. Find Peak Element
32:53
take U forward
Рет қаралды 198 М.
Sqrt(x) - Leetcode 69 - Python
8:34
NeetCodeIO
Рет қаралды 68 М.
5 Math Skills Every Programmer Needs
9:08
Sahil & Sarra
Рет қаралды 1,1 МЛН
BS-18. Allocate Books or Book Allocation | Hard Binary Search
27:29
take U forward
Рет қаралды 183 М.
BS-13. Minimum days to make M bouquets | Binary Search
26:01
take U forward
Рет қаралды 152 М.
BS-16. Kth Missing Positive Number | Maths + Binary Search
22:52
take U forward
Рет қаралды 158 М.
BS-12. Koko Eating Bananas
21:04
take U forward
Рет қаралды 186 М.
Мама у нас строгая
00:20
VAVAN
Рет қаралды 10 МЛН