Nth Root of a Number Using Binary Search

  Рет қаралды 109,092

take U forward

take U forward

Күн бұрын

Пікірлер: 190
@takeUforward
@takeUforward 3 жыл бұрын
Instead of multiplying by running a for loop, we can use Binary Exponentiation technique to do in log N. Video: kzbin.info/www/bejne/omG8dGZubJukrsk
@secondguy
@secondguy 3 жыл бұрын
so sir, should the time complexity be logN*log(M*10^d)??
@pranavgawade1546
@pranavgawade1546 3 жыл бұрын
You shoud not sacrifise your sleep for this.
@Usurperhk
@Usurperhk 3 жыл бұрын
Can't believe you work late nights to deliver quality content in community. That's what makes you different and an inspiration to many students.
@Ben10qz
@Ben10qz 3 жыл бұрын
Fun fact : Scalar is showing their add here also in between the video !
@Mr_SSK
@Mr_SSK 2 жыл бұрын
We can reduce the search space by taking 'high=m/2' initially. But, here we need to handle the case of n==1 explicitly, that means when n==1 ( we want the 1st root of m) we have to directly return m. We won't go for any calculations. -> We can set high=sqrt(m) as well, but in that case you have to make sure the sqrt() returns double (instead of int) Anyways, AMAZING EXPLANATION! 💜🙏
@vanshikagarg489
@vanshikagarg489 3 жыл бұрын
Overall complexity is N Log(N) by doing... We can also count the the number of element which are smaller than mid in O(n + n ) . Instead of doing binary search on every row we can compare last element of every row if it it smaller than mid we take count of row size or if it is greater we can traverse the row by back until elem is bigger than mid. This takes O( n + n ) time.
@vanshsehgal9475
@vanshsehgal9475 2 жыл бұрын
what do you mean by row?
@lifegood6401
@lifegood6401 3 жыл бұрын
1 clock waali baat ne emotional kr diya. Great Explanation. Mja aa jata hai aapki videos dekh kr. I am confident i am going to do good in placements if i finish this series honestly
@sanbidrc
@sanbidrc 3 жыл бұрын
We can never thank you enough for such amazing content.
@OatsAurCode
@OatsAurCode Жыл бұрын
DP series is helpul. Somehow the code not passing all the test cases in gfg. So adding a working code. That might be helpful class Solution: def NthRoot(self, n, m): # Code here if m == 1: return 1 if n==0: return 1 if n==1: return m start = 0 end = m//2 while start
@bhaveshkumar6842
@bhaveshkumar6842 3 жыл бұрын
There's nobody like you, Striver. You're the BEST!!!!
@govindsharma7696
@govindsharma7696 3 жыл бұрын
Bhaiya aap jadugar ho , kya samjhate ho mashallah ❤️❤️
@AbidAhsan-yp4dc
@AbidAhsan-yp4dc 3 жыл бұрын
this is the best explanation of the problem you can find on entire internet . thanks a lot
@jayachandra677
@jayachandra677 3 жыл бұрын
I thought this would be just like mid*mid == n but I didn't know that decimal thing. Also I blindly thought that the time complexity will be O(n*log(n)). Thanks for the video bhaiya!!!
@amarks444
@amarks444 2 жыл бұрын
i am watching this video at 2:04am 🥱 , and when i heared "recording video at 1:00 am and sleep at 4:00 am after editing" , my energy get boosted 😎🤩 ....
@senpaidawwwggg
@senpaidawwwggg Жыл бұрын
2:51 LoL
@arnabghosh9632
@arnabghosh9632 2 жыл бұрын
We can use binary exponentiation or can use inbuilt pow function to calculate the power this will reduce the TC to O(LogN * log(M*decimal*10)
@akankshamaurya9219
@akankshamaurya9219 2 жыл бұрын
But using inbuilt pow function ,it is not passing all test cases
@immortal4412
@immortal4412 Жыл бұрын
@@akankshamaurya9219 it is working check in gfg practice
@pratyushbhatt1712
@pratyushbhatt1712 3 жыл бұрын
Great!! You can further optimize the solution to a much greater extent bdw. When using the multiply function, you are using a very naive technique to calculate the Power, instead we can use Binary Exponentiation, which is logarithmic in nature. That will fasten it up. Great explanation bdw.
@takeUforward
@takeUforward 3 жыл бұрын
Yeah I have mentioned in the pinned comment :)
@pratyushbhatt1712
@pratyushbhatt1712 3 жыл бұрын
@@takeUforward yeah just saw that.. Anyways, great content as always.
@aandz6194
@aandz6194 3 жыл бұрын
@@takeUforward Im little confused because..... Either we get the root of the number don't we check until 10^(-6)....... So what will be time complexity now in the worst case.....?
@sonuGupta-vo8vm
@sonuGupta-vo8vm 2 жыл бұрын
Always learn new concept . And you makes CP more interesting . I likes your way of explaining time and space complexity. Thanks
@kartikgarg9379
@kartikgarg9379 3 жыл бұрын
Bhaiya your graph series is amazing and those dry runs are awesome thanks bhaiya
@RamKumar-kz8gg
@RamKumar-kz8gg 3 жыл бұрын
enjoy
@411_danishranjan9
@411_danishranjan9 2 жыл бұрын
why you take low = mid, why not low= mid+1 and high = mid, why not high = mid-1
@deepraj2631
@deepraj2631 2 жыл бұрын
As you can see that he took 1 base indexing not zero base indexing, hope you can get the idea why it's happening so
@harshabellamkondasrinivasa4527
@harshabellamkondasrinivasa4527 3 жыл бұрын
omg.....4'o clock in the morning.... hats off to ur dedication sir. Can't thank u enough
@parthparmar9798
@parthparmar9798 2 жыл бұрын
this is what I want. an amazing playlist for Binary search.
@jothamprince8765
@jothamprince8765 2 жыл бұрын
thanks man, u just saved me from failing my class assignment, i've been trying to crack that for 3 days
@krishnakumar-rp9wc
@krishnakumar-rp9wc 3 жыл бұрын
can we use pow instead of multiply function?
@kaichang8186
@kaichang8186 Ай бұрын
understood, thanks for the great explanation
@ReD4eva94
@ReD4eva94 3 жыл бұрын
I have seen a lot on youtube. You are the very best.
@harleenkaur7751
@harleenkaur7751 Жыл бұрын
Thanks a lot striver. We always learn a new concept through your lecture.
@prithvichetty15
@prithvichetty15 3 жыл бұрын
Best Explanation of Binary Search. Eagerly Waiting for your Tree Series!!!❤❤
@yeswanthh5068
@yeswanthh5068 2 жыл бұрын
Thank you sir understood
@harshitvarshney2627
@harshitvarshney2627 3 жыл бұрын
Great Explanation bhaiya Bhaiya....please make a playlist on the tree as well.
@cpwithsundar
@cpwithsundar 3 жыл бұрын
There is no match for your hardwork , but we too would work hard even if it doesn't surpasses yours.
@dayanandraut5660
@dayanandraut5660 3 жыл бұрын
Great explanation around intuition building and time complexity. Keep it up bro
@manavmalhotra8513
@manavmalhotra8513 2 жыл бұрын
public class Solution { public static double findNthRootOfM(int n, long m) { // Write your code here. double lo=1; double hi=m; double eps=1e-7; while((hi-lo) > eps){ double mid = (lo+hi)/2.0; if(multiply(mid,n)
@dipanshusharma9084
@dipanshusharma9084 2 жыл бұрын
make these changes cout
@muhsinmansooriee_0017
@muhsinmansooriee_0017 Жыл бұрын
@@dipanshusharma9084 why did we do this? as it is asking in the question to check for up to 6 decimal points.
@dipanshusharma9084
@dipanshusharma9084 Жыл бұрын
@@muhsinmansooriee_0017 because in the ans it was taking upto 9 decimals , there was mistake in test cases
@prakharanand7778
@prakharanand7778 Жыл бұрын
because when you are passing the argument to the multiply method, you are storing mid in parameter n and n in parameter m method call. But you are running the for loop inside multiply() n times which actually is mid due to your incorrect argument order during method call. fix it and your code will work just fine.
@gather.in.coding.lounge
@gather.in.coding.lounge 2 жыл бұрын
You are just doing all the way great, I really thank you for all these wonderful playlists.
@prakharanand7778
@prakharanand7778 Жыл бұрын
In time complexity from 1 to say 27 there aren't 10*27 real numbers for upto 1 decimal place. 1.0 1.1 1.2 .... 1.9 (10 nos) 2.0 2.1 ... 2.9 (10 nos) ... 26.0 26.1...26.9 (10 nos) total nos till now = 10*26. And one more number 27.0 which makes the total real numbers from 1 to 27 equal to 10*26 + 1 => (M-1)*10 + 1? If no please explain.
@parthkabra8880
@parthkabra8880 3 жыл бұрын
no words, excellent explanation
@RohitKumar-dy2gc
@RohitKumar-dy2gc Жыл бұрын
u r so dedicated man i wanna be also that much
@AkshayKumar-kp2ye
@AkshayKumar-kp2ye 2 жыл бұрын
My thought process went towards the mathematics approach initially. Wasn't able to deduce the binary search approach on this. Thanks for the video bro. double findNthRootOfM(int n, int m) { // Write your code here. return exp(log(m)/n); }
@antiquefood6284
@antiquefood6284 3 жыл бұрын
Recently asked in Apple Interview to my experienced brother !!!
@shaamidrees6036
@shaamidrees6036 2 жыл бұрын
amazing explaination as always Thanks
@yogeshyts
@yogeshyts 2 жыл бұрын
double t=1.000000/n; double ans=pow(m,t); return ans;
@monkeeys23
@monkeeys23 3 жыл бұрын
hey striver could you make a video for a non engineering background student to start coding journey and get into top companies road map sort of thing
@RamKumar-kz8gg
@RamKumar-kz8gg 3 жыл бұрын
watch love babber video
@ShwetaSingh-qw7mo
@ShwetaSingh-qw7mo 2 жыл бұрын
great explanation
@amitzerikunthe7635
@amitzerikunthe7635 Жыл бұрын
hats off to ur dedication 🙏
@Ballistic_Bytes
@Ballistic_Bytes Жыл бұрын
A much simpler method of solving it would be to just use log and antilog. Following is a TypeScript Solution:- const nthRootOfM = ( n: number, m: number, ): number => Math.exp(Math.log(m) / n);
@harshitsaxena7205
@harshitsaxena7205 Жыл бұрын
do you have any question on std platform please provide me for practise
@jayadubey_22
@jayadubey_22 3 жыл бұрын
keep educating bhaiya nicely explained
@sourabhchoudhary7289
@sourabhchoudhary7289 3 жыл бұрын
why can't we put an extra condition for multiply(mid,n)==m then return mid?
@SudhanshuKumar-lp1nr
@SudhanshuKumar-lp1nr 3 жыл бұрын
Because it will take a lot more iteration to become equal to root(M).
@srijitmondal4190
@srijitmondal4190 3 жыл бұрын
@@SudhanshuKumar-lp1nr why? We are not putting the condition inside while loop instead we are putting this as an extra if()
@sauravkumargupta2594
@sauravkumargupta2594 3 жыл бұрын
Ye jo unlike krne wale hai na... Unka mental level hi nhi itni asan videos ko smjhne ka 😂😂
@s.g.prajapati3597
@s.g.prajapati3597 2 жыл бұрын
Great content bhaiya! Learned and understood to the fullest!
@willturner3440
@willturner3440 3 жыл бұрын
Sde sheet become interesting day by day
@aaaa-pr2dd
@aaaa-pr2dd 3 жыл бұрын
😳awesome dada, didn't knew to use binary search this way
@saurabh44gupta
@saurabh44gupta 3 жыл бұрын
Hi, I want to know why we need to apply Dijkstra to find the shortest path when we find the shortest path using BFS traversal.
@RamKumar-kz8gg
@RamKumar-kz8gg 3 жыл бұрын
following
@striver_79
@striver_79 3 жыл бұрын
BFS will take longer since it does not follows the greedy method of choosing the shorter path, so you will end up pushing any node a number of times into the queue actually.. which will end up taking a lot of time, over here, you always start with the shortest node reachable and then try to move ahead..
@darkamv-x5822
@darkamv-x5822 2 жыл бұрын
You are best
@pawanlok1776
@pawanlok1776 2 жыл бұрын
thank you for making this video ,
@techcode356
@techcode356 Жыл бұрын
Thank You!
@bhaveshkumar6842
@bhaveshkumar6842 3 жыл бұрын
Thank you, Striver!!!!!!!!!!
@sarthaksinghrajput5462
@sarthaksinghrajput5462 3 жыл бұрын
Thank you Bhaiya!
@nileshsinha7869
@nileshsinha7869 3 жыл бұрын
Great explanation (as always)👌🙌
@mayankkumar4864
@mayankkumar4864 3 жыл бұрын
bhaiya, can you please explain that, why didn't you return the value of "low" or "high" from the function double getNthRoot , as it will be the value of the root ?
@CodePinaka
@CodePinaka 2 жыл бұрын
understood
@ayushbisht2689
@ayushbisht2689 3 жыл бұрын
Best explanation.
@funenjoynilaypatel4553
@funenjoynilaypatel4553 2 жыл бұрын
9 march 2022 9:39 pm
@aandz6194
@aandz6194 3 жыл бұрын
Im little confused because..... Either we get the root of the number don't we check until 10^(-6)....... So what will be time complexity now in the worst case.....?
@lakshmiprasanna7058
@lakshmiprasanna7058 Жыл бұрын
Understood 💯💯💯
@jotaroackerman3447
@jotaroackerman3447 3 жыл бұрын
amazing video bhaiya
@s_3095
@s_3095 Жыл бұрын
thanks bhai striver
@shubhambhardwaj8894
@shubhambhardwaj8894 3 жыл бұрын
Thanks
@shubhamnalawade1606
@shubhamnalawade1606 3 жыл бұрын
Outstanding explanation
@shubhamnalawade1606
@shubhamnalawade1606 3 жыл бұрын
Could you please make videos solving coding problems in contests
@son_of_a_dentist8107
@son_of_a_dentist8107 Жыл бұрын
very good video
@antrapurohit8010
@antrapurohit8010 2 жыл бұрын
great solution. but it is not accepted in coding ninja platform
@AbhishekKumar-td5zu
@AbhishekKumar-td5zu 3 жыл бұрын
tqsm bhaiya
@radharamamohanakunnattur3035
@radharamamohanakunnattur3035 Жыл бұрын
Understtood!! and a Big Thanks!!
@dhruvmittal4992
@dhruvmittal4992 3 жыл бұрын
why the search space is not 1 to 13 in first example ?
@abcsumits
@abcsumits 2 жыл бұрын
what about interpolation search?
@yashwagh83
@yashwagh83 3 жыл бұрын
Thanks for your efforts and sleepless nights. You are doing god's work Raj.
@ankitkholiya4305
@ankitkholiya4305 2 жыл бұрын
Thanks bro you are great
@srujanat483
@srujanat483 2 жыл бұрын
Understood
@tejaswi4650
@tejaswi4650 3 жыл бұрын
How did you decide we need to stop at 1e - 6?
@prakharagarwal6237
@prakharagarwal6237 3 жыл бұрын
it will be given in the question itself
@shivansh8006
@shivansh8006 2 жыл бұрын
Why we can not use this " return pow(2, log2(m) / n); ". It will give the same answer :/
@shivansh8006
@shivansh8006 2 жыл бұрын
Bhai ye btado yar koi, iski time complexity kam hai na compare to the solution given in the video??
@sonushaw3715
@sonushaw3715 2 жыл бұрын
Thank you bhaia love you❤
@prateekverma9166
@prateekverma9166 3 жыл бұрын
Can anybody tell why we haven`t used low=mid+1 or high=mid-1
@takeUforward
@takeUforward 3 жыл бұрын
Because we want annswer in terms of decimal places, doing a -1 will not help :)
@prateekverma9166
@prateekverma9166 3 жыл бұрын
@@takeUforward Got it ,thanks Striver
@Sksahu_123
@Sksahu_123 3 жыл бұрын
Bhaiya jisme setprecision use karte so wale bhi question upload kardo
@kushalchakraborty8615
@kushalchakraborty8615 Жыл бұрын
Great content
@umarqureshi8499
@umarqureshi8499 3 жыл бұрын
so good bhai!!!!!!!!!
@ahishnar1568
@ahishnar1568 3 жыл бұрын
Really nice explanation striver.
@takeUforward
@takeUforward 3 жыл бұрын
Pura th dekh lo 😂
@ishankbansal9239
@ishankbansal9239 3 жыл бұрын
@@takeUforward 🤣🤣🤣🤣🤣🤣🤣🤣
@ahishnar1568
@ahishnar1568 3 жыл бұрын
@@takeUforward Pehle se pata hai sir Awesome hi hone wali hai. I am your current batch student. That is why I know. 😅☺️🤘
@Steve-kv5we
@Steve-kv5we Жыл бұрын
Understood💯💯
@thrilokponnuru3131
@thrilokponnuru3131 2 жыл бұрын
Why the search space start from 1? What happens when m value is negative
@shubhamrana1776
@shubhamrana1776 2 жыл бұрын
Sir in the constraints N=300 and since we are searching in 1 to m , Why the value x ^300 not overflowing while calculating the power? Please help
@amitkumaryadav7240
@amitkumaryadav7240 2 жыл бұрын
Can anyone explain why time complexity is not n×log(m)
@AnkitMishra-mz4xt
@AnkitMishra-mz4xt 3 жыл бұрын
Eagerly wanting
@codeguy21
@codeguy21 3 жыл бұрын
@striver , please use this channel also to i.e in english about how u do in your hindi channel ! that would be great for non hindi speakers ! thanks
@shubhamsinha2362
@shubhamsinha2362 Жыл бұрын
Why can't we just use the Pow function?
@ayushkapri9082
@ayushkapri9082 3 жыл бұрын
why search space is not reduced to mid - 1, why just mid ??
@takeUforward
@takeUforward 3 жыл бұрын
Because mid could be your answer..
@SatyamKumar-bw4vi
@SatyamKumar-bw4vi 2 жыл бұрын
Hare Krishna !🙂
@bilalkhan2298
@bilalkhan2298 2 жыл бұрын
Can't thank u enough
@Tommyshelby602
@Tommyshelby602 Жыл бұрын
low = 0 to pass 11th tc on coding ninja
@ng3w462
@ng3w462 3 жыл бұрын
Can I achieve command on full DSA in 6 months?
@RamKumar-kz8gg
@RamKumar-kz8gg 3 жыл бұрын
you can do whatever you can imagine
@ng3w462
@ng3w462 3 жыл бұрын
@@RamKumar-kz8gg thanks for the motivation bro...🤟
@JackkofAll
@JackkofAll 3 жыл бұрын
Raj bhaiya is there a discussion group for your channel on telegram ? If not please create one , it would be great to discuss stuff among all of us
@takeUforward
@takeUforward 3 жыл бұрын
link me h na
@VishalKumar-xm5ue
@VishalKumar-xm5ue 3 жыл бұрын
I have a doubt given this question: 1st in technical round : If the company wants a "coder" then y cant we use "pow" function 2nd in interview round: If the interviewer asks and we write him 2 lines code of using same "pow" will he be like no i want some other technique?(I assume they will ask....because i havent faced too many interviews yet...
@bhupeshbhatt4849
@bhupeshbhatt4849 2 жыл бұрын
Can anyone explain why we have set low = mid and high = mid and not low = mid +1 and high =mid -1
@VivekKumar-zo7kr
@VivekKumar-zo7kr 2 жыл бұрын
lets suppose from striver explanation, mid was 14. so he not moved to mid-1 which is 13 because 13.1 to 13.9 might also be a possible answer. in the same way, lets suppose mid was 3 and if we had moved to mid-1 i.e. 2, then we will not be able to access 2.1 to 2.9 which might be an answer..
@pk4288
@pk4288 Жыл бұрын
@@VivekKumar-zo7kr Thanks for this explanation.. I was not able to get this part :)
@namanchandak1532
@namanchandak1532 Жыл бұрын
My this code require less no of calculations because whenever I found the number I am going to return it And in your code if 'l' pointer is on the ans then your *r* first approach to the pointer then it will leave the while loop and again calculate the ans then it will return int NthRoot(int n, int m) { // Write your code here. int l=1,r=m; while(l
@shivamgupta-fm8qs
@shivamgupta-fm8qs 2 жыл бұрын
US
Median of Row Wise Sorted Matrix | Nested Binary Search
28:42
take U forward
Рет қаралды 138 М.
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
I thought one thing and the truth is something else 😂
00:34
عائلة ابو رعد Abo Raad family
Рет қаралды 9 МЛН
МЕНЯ УКУСИЛ ПАУК #shorts
00:23
Паша Осадчий
Рет қаралды 5 МЛН
How Many Balloons To Make A Store Fly?
00:22
MrBeast
Рет қаралды 150 МЛН
Why no RONALDO?! 🤔⚽️
00:28
Celine Dept
Рет қаралды 88 МЛН
BS-11. Find the Nth root of an Integer
20:44
take U forward
Рет қаралды 130 М.
BS-10. Finding Sqrt of a number using Binary Search
17:11
take U forward
Рет қаралды 145 М.
Single Element In Sorted Array | Leetcode
12:42
take U forward
Рет қаралды 77 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 675 М.
Median of two Sorted Arrays of Different Sizes | Binary Search
31:54
take U forward
Рет қаралды 240 М.
C++ vs Rust: which is faster?
21:15
fasterthanlime
Рет қаралды 404 М.
Why is Python 150X slower than C?
10:45
Mehul - Codedamn
Рет қаралды 21 М.
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 199 М.
BS-14. Find the Smallest Divisor Given a Threshold | Binary Search
16:00
I thought one thing and the truth is something else 😂
00:34
عائلة ابو رعد Abo Raad family
Рет қаралды 9 МЛН