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

  Рет қаралды 105,934

take U forward

take U forward

Жыл бұрын

Problem Link: bit.ly/3J5dBCJ
Notes/C++/Java/Python codes: takeuforward.org/binary-searc...
We have solved the problem, and we have gone from brute force and ended with the most optimal solution. Every approach's code has been written in the video itself. Also, we have covered the algorithm with intuition.
Full Course: bit.ly/tufA2ZYt
You can follow me across social media, all my handles are below:
Linkedin/Instagram/Telegram: linktr.ee/takeUforward
0:00 Introduction of Course

Пікірлер: 193
@varun1017
@varun1017 5 ай бұрын
The check "mid * mid
@anigaming7146
@anigaming7146 5 ай бұрын
Thanks bro😊
@varun1017
@varun1017 5 ай бұрын
@@anigaming7146 welcome
@pushankarmakar1783
@pushankarmakar1783 5 ай бұрын
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 5 ай бұрын
@@pushankarmakar1783 all the best
@harshitrautela6585
@harshitrautela6585 4 ай бұрын
Thanks for help.❤❤
@piyushraj5464
@piyushraj5464 Жыл бұрын
Had been desperately waiting for the series to restart. Thanks a lot.
@Dipanshutripathi2407
@Dipanshutripathi2407 10 ай бұрын
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.
@cinime
@cinime Жыл бұрын
Understood! Awesome explanation as always, thank you very very much for your effort!!
@souviksen7345
@souviksen7345 Жыл бұрын
Always top level concept clearer ❤
@saireddyreddy8969
@saireddyreddy8969 5 ай бұрын
Your explanation is just awesome.
@tripd4949
@tripd4949 2 ай бұрын
Wow that was a very slick approach, good job.
@user-ti3bd8mp1w
@user-ti3bd8mp1w Жыл бұрын
understood.. Thank you striver for such an amazing explanation
@SuvradipDasPhotographyOfficial
@SuvradipDasPhotographyOfficial Жыл бұрын
amazing explanation striver, understood.
@ashishpradhan6250
@ashishpradhan6250 Ай бұрын
understood clearly...thanks a lot, u have given me hope!
@manavsingh5919
@manavsingh5919 11 ай бұрын
Thank you Striver...Understood everything🙂
@adarshkumarrao3478
@adarshkumarrao3478 Жыл бұрын
Excellent explanation ❣.. Waiting for linked list series💛💛
@Manasidas99
@Manasidas99 10 ай бұрын
Great video you made thank you very much sir waiting for the linked list Video.
@shreyanshkumar4528
@shreyanshkumar4528 11 ай бұрын
Just awesome explanation.
@hareshnayak7302
@hareshnayak7302 3 ай бұрын
Understood, Thanks striver for this amazing video.
@AbhiSharma-ku7dq
@AbhiSharma-ku7dq 20 күн бұрын
Thank you for creating such courses
@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; }
@srilathareddy9450
@srilathareddy9450 Жыл бұрын
Superb explanation
@pragathid7743
@pragathid7743 3 ай бұрын
awesome explanation sir
@AnmolGupta-oj4lm
@AnmolGupta-oj4lm 11 ай бұрын
Understood Very Well!
@subhankarkanrar9494
@subhankarkanrar9494 5 ай бұрын
Respect to Striver ❤ for such a easy explanation
@sukhpreetsingh5200
@sukhpreetsingh5200 Жыл бұрын
❤Awesome as usual
@lucario404
@lucario404 Жыл бұрын
we can check sqrt from 1(low) to n/2 (high) as well
@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.
@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
@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 8 ай бұрын
@@Shanky_17 Just love him for that
@NazeerBashaShaik
@NazeerBashaShaik 3 ай бұрын
Understood, thank you.
@ishasingh6726
@ishasingh6726 Жыл бұрын
Thank youuu striver❤
@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 Ай бұрын
i did the same initially
@EC20022ELAKKIYAC
@EC20022ELAKKIYAC 6 ай бұрын
Understood!!! Thank You
@syedFAHIM-el1wr
@syedFAHIM-el1wr Жыл бұрын
Thanks you a lot, Can't thank enough
@livinkumarsaravanan3781
@livinkumarsaravanan3781 9 күн бұрын
Worth content 👍
@vigneshlion9309
@vigneshlion9309 7 ай бұрын
great video!❣
@UddhikaIshara
@UddhikaIshara 9 ай бұрын
Thank you very much
@user-or5oz1pk2x
@user-or5oz1pk2x 2 ай бұрын
Thanks a lot Bhaiya
@Aditi-do4im
@Aditi-do4im 7 ай бұрын
you r incredible !!!!!!!!!!
@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 9 ай бұрын
What about 2?
@dhruvchopra26
@dhruvchopra26 6 ай бұрын
@@abhimanyushekhawat2626 Works for 2 as well.
@artifice_abhi
@artifice_abhi 6 ай бұрын
just right an if condition at the start @@abhimanyushekhawat2626
@pushankarmakar1783
@pushankarmakar1783 5 ай бұрын
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.
@VishalGupta-xw2rp
@VishalGupta-xw2rp 9 ай бұрын
10:50 Opposite Polarity Lec 12, 13 also has
@YourCodeVerse
@YourCodeVerse 7 ай бұрын
Understood✅🔥🔥
@parulvats
@parulvats Жыл бұрын
In pseudo code it was mentioned either we can return ans or high, but returning ans giving error
@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 11 ай бұрын
hows your dsa prep going now , i just started , does it get any better?🤣
@impalash_ag
@impalash_ag 14 күн бұрын
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 12 күн бұрын
@@impalash_ag good observation
@himanshukamble765
@himanshukamble765 3 күн бұрын
yup i also thought of that
@amanasrani6405
@amanasrani6405 14 күн бұрын
Understood Bhaiya
@sparrow_harsh
@sparrow_harsh 10 ай бұрын
Can you come up with a video where we need to find square root upto decimal precision p
@raholsaha8191
@raholsaha8191 Жыл бұрын
great video
@MohammedHasmi577
@MohammedHasmi577 3 ай бұрын
❤❤great video bro
@hrushi_borhade
@hrushi_borhade Жыл бұрын
Understood striver!!
@user-is6ky7pp2n
@user-is6ky7pp2n Ай бұрын
Understood !! 😎😎
@Josuke217
@Josuke217 3 күн бұрын
Binary Search on answers finally !
@user-nb1ye5tf1r
@user-nb1ye5tf1r 6 ай бұрын
Understood
@md.imrankhan6912
@md.imrankhan6912 10 ай бұрын
Legendary boss
@thenriquevicentini
@thenriquevicentini 2 ай бұрын
Understood!
@utsavseth6573
@utsavseth6573 Жыл бұрын
Understood RAJ.
@senseiAree
@senseiAree 9 ай бұрын
Understood ❤
@mcbotface
@mcbotface Жыл бұрын
Largest integer or smallest integer are correct terminologies.
@kushagramishra5638
@kushagramishra5638 Жыл бұрын
understood!
@sarangkumarsingh7901
@sarangkumarsingh7901 2 ай бұрын
Understood.......
@crazymemes4080
@crazymemes4080 Жыл бұрын
Understood bhayaaaaaaaa
@mehedihasansabbir7886
@mehedihasansabbir7886 10 ай бұрын
Superb
@kumarshivam3661
@kumarshivam3661 Жыл бұрын
Striver Bhaiya please august end tak a to z sheet complete kra do
@RaviKumar-sn6tu
@RaviKumar-sn6tu 3 ай бұрын
understood!!
@VineetKumar-fk2rl
@VineetKumar-fk2rl 6 ай бұрын
Understood❤
@rhul0017
@rhul0017 9 ай бұрын
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?
@VishalGupta-xw2rp
@VishalGupta-xw2rp 9 ай бұрын
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 6 ай бұрын
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.
@arihantjammar8888
@arihantjammar8888 11 ай бұрын
UNDERSTOOD
@Spavan3011
@Spavan3011 2 ай бұрын
What if we have to find exact ans not an integer?
@parassetia4964
@parassetia4964 24 күн бұрын
Understood boss^^
@AmanKumar-qz4jz
@AmanKumar-qz4jz 11 ай бұрын
thank u sir
@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 😞😞😞
@ambaradhikari7425
@ambaradhikari7425 Жыл бұрын
waiting for linked list, bit manipulation and strings please
@mrsttechno5974
@mrsttechno5974 10 ай бұрын
Understand
@dreamyme543
@dreamyme543 9 ай бұрын
Understood🙃
@sumanth2888
@sumanth2888 9 ай бұрын
understood
@kiransoorya7895
@kiransoorya7895 Ай бұрын
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
@nayankhuman1043
@nayankhuman1043 9 күн бұрын
Understood :)
@rajashekar9912
@rajashekar9912 Жыл бұрын
Bhaiya plz start linked list series
@SatyaPrakash-dj8ix
@SatyaPrakash-dj8ix 2 ай бұрын
this is somewhat similar to Newton-Raphson method we use in engineering mathematics.
@roshangupta1686
@roshangupta1686 7 ай бұрын
underatood
@pihus3498
@pihus3498 Жыл бұрын
understood :)❤
@keshavbiyani9202
@keshavbiyani9202 Ай бұрын
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.
@thedominatorscircle6762
@thedominatorscircle6762 9 ай бұрын
Bhi logic kayse build Karu kuch batao plzzzz
@Icelander00
@Icelander00 Жыл бұрын
Thanks
@undextered
@undextered Жыл бұрын
Great
@astroop1528
@astroop1528 10 ай бұрын
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
@akbunofficial1281
@akbunofficial1281 9 ай бұрын
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 7 ай бұрын
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 6 ай бұрын
mine was failing for n = 1, as I had my upper bound at n/2 which was rounding it down to 0.
@jimmy-qq78
@jimmy-qq78 Ай бұрын
@@Simran-ns8gp i also changed but it is not working pls tell
@mihirkumar4770
@mihirkumar4770 4 ай бұрын
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
@352_sanyajain4
@352_sanyajain4 Жыл бұрын
nicee
@BiswajitDas-fj5gp
@BiswajitDas-fj5gp Жыл бұрын
❤❤❤
@Vishnu-zk2nm
@Vishnu-zk2nm 11 ай бұрын
Which notes app you are using ???
@motabhaimotivation
@motabhaimotivation 9 ай бұрын
"good notes " that comes in ipads
@princepandey1836
@princepandey1836 5 ай бұрын
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
@srj8dmusic699
@srj8dmusic699 Жыл бұрын
First View ❤🎉
@nikhilkumarjamwal5322
@nikhilkumarjamwal5322 8 ай бұрын
square root with precision ???
@official_madhusudan_chauhan
@official_madhusudan_chauhan Ай бұрын
Present Sir 25th/06
@-ShangsitNath
@-ShangsitNath 3 ай бұрын
What is the issue in this code?? int floorSqrt(int n) { // Write your code here. int ans=-1; int s=1, e=n; while(s
@bytesage-he8pd
@bytesage-he8pd 2 ай бұрын
Same for me. Test cases are hidden also
@bytesage-he8pd
@bytesage-he8pd 2 ай бұрын
The two corner case are n == 0 and. n == 1 add check for them it will pass. Thanks @mihirkumar4770
@user-tk9ks8rs4g
@user-tk9ks8rs4g 11 ай бұрын
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 10 ай бұрын
just to test our problem solving skills
@girikgarg8
@girikgarg8 10 ай бұрын
Done
@aakashbhandari9761
@aakashbhandari9761 10 ай бұрын
What is the number is not perfect square??
@girikgarg8
@girikgarg8 10 ай бұрын
@@aakashbhandari9761 Return the floor value i.e. nearest integer to the left
@aakashbhandari9761
@aakashbhandari9761 10 ай бұрын
@@girikgarg8 means if the number is 28 we should return 5.0??
@user-uv5or5bm2c
@user-uv5or5bm2c Ай бұрын
UnderStood
@cenacr007
@cenacr007 9 ай бұрын
us
BS-11. Find the Nth root of an Integer
20:44
take U forward
Рет қаралды 95 М.
Эффект Карбонаро и нестандартная коробка
01:00
История одного вокалиста
Рет қаралды 9 МЛН
Stay on your way 🛤️✨
00:34
A4
Рет қаралды 6 МЛН
Женская драка в Кызылорде
00:53
AIRAN
Рет қаралды 385 М.
I Can't Believe We Did This...
00:38
Stokes Twins
Рет қаралды 131 МЛН
BS-9. Find Peak Element
32:53
take U forward
Рет қаралды 144 М.
BS-17. Aggressive Cows | Binary Search Hard
26:44
take U forward
Рет қаралды 130 М.
BS-18. Allocate Books or Book Allocation | Hard Binary Search
27:29
take U forward
Рет қаралды 137 М.
The Plans to Assassinate Hitler
12:26
fern
Рет қаралды 1,3 МЛН
BS-12. Koko Eating Bananas
21:04
take U forward
Рет қаралды 135 М.
Every War Strategy Explained in 8 Minutes
8:42
The Paint Explainer
Рет қаралды 88 М.
Эффект Карбонаро и нестандартная коробка
01:00
История одного вокалиста
Рет қаралды 9 МЛН