Nth Root of a Number Using Binary Search

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ก.ย. 2024

ความคิดเห็น • 189

  • @takeUforward
    @takeUforward  3 ปีที่แล้ว +43

    Instead of multiplying by running a for loop, we can use Binary Exponentiation technique to do in log N. Video: th-cam.com/video/l0YC3876qxg/w-d-xo.html

    • @secondguy
      @secondguy 3 ปีที่แล้ว +3

      so sir, should the time complexity be logN*log(M*10^d)??

    • @pranavgawade1546
      @pranavgawade1546 3 ปีที่แล้ว +2

      You shoud not sacrifise your sleep for this.

  • @Usurperhk
    @Usurperhk 3 ปีที่แล้ว +80

    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 ปีที่แล้ว +22

    Fun fact : Scalar is showing their add here also in between the video !

  • @Mr_SSK
    @Mr_SSK 2 ปีที่แล้ว +8

    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 ปีที่แล้ว +5

    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.

  • @lifegood6401
    @lifegood6401 3 ปีที่แล้ว +4

    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 ปีที่แล้ว +19

    We can never thank you enough for such amazing content.

  • @bhaveshkumar6842
    @bhaveshkumar6842 2 ปีที่แล้ว +2

    There's nobody like you, Striver. You're the BEST!!!!

  • @pratyushbhatt1712
    @pratyushbhatt1712 3 ปีที่แล้ว +5

    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 ปีที่แล้ว +5

      Yeah I have mentioned in the pinned comment :)

    • @pratyushbhatt1712
      @pratyushbhatt1712 3 ปีที่แล้ว

      @@takeUforward yeah just saw that.. Anyways, great content as always.

    • @aandz6194
      @aandz6194 2 ปีที่แล้ว

      @@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.....?

  • @amarks444
    @amarks444 2 ปีที่แล้ว +9

    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 😎🤩 ....

  • @411_danishranjan9
    @411_danishranjan9 2 ปีที่แล้ว +7

    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

  • @AbidAhsan-yp4dc
    @AbidAhsan-yp4dc 3 ปีที่แล้ว

    this is the best explanation of the problem you can find on entire internet . thanks a lot

  • @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

  • @krishnakumar-rp9wc
    @krishnakumar-rp9wc 3 ปีที่แล้ว +6

    can we use pow instead of multiply function?

  • @jayachandra677
    @jayachandra677 3 ปีที่แล้ว +1

    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!!!

  • @sonuGupta-vo8vm
    @sonuGupta-vo8vm ปีที่แล้ว

    Always learn new concept . And you makes CP more interesting . I likes your way of explaining time and space complexity. Thanks

  • @kartikgarg9379
    @kartikgarg9379 3 ปีที่แล้ว +6

    Bhaiya your graph series is amazing and those dry runs are awesome thanks bhaiya

  • @harshabellamkondasrinivasa4527
    @harshabellamkondasrinivasa4527 2 ปีที่แล้ว +1

    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.

  • @govindsharma7696
    @govindsharma7696 3 ปีที่แล้ว +2

    Bhaiya aap jadugar ho , kya samjhate ho mashallah ❤️❤️

  • @jothamprince8765
    @jothamprince8765 2 ปีที่แล้ว

    thanks man, u just saved me from failing my class assignment, i've been trying to crack that for 3 days

  • @prithvichetty15
    @prithvichetty15 3 ปีที่แล้ว

    Best Explanation of Binary Search. Eagerly Waiting for your Tree Series!!!❤❤

  • @prakharanand7778
    @prakharanand7778 9 หลายเดือนก่อน

    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.

  • @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

  • @dayanandraut5660
    @dayanandraut5660 3 ปีที่แล้ว +1

    Great explanation around intuition building and time complexity. Keep it up bro

  • @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.

  • @AkshayKumar-kp2ye
    @AkshayKumar-kp2ye ปีที่แล้ว

    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);
    }

  • @monkeeys23
    @monkeeys23 3 ปีที่แล้ว +5

    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

  • @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.

  • @harshitvarshney2627
    @harshitvarshney2627 3 ปีที่แล้ว

    Great Explanation bhaiya
    Bhaiya....please make a playlist on the tree as well.

  • @parthkabra8880
    @parthkabra8880 3 ปีที่แล้ว +1

    no words, excellent explanation

  • @RohitKumar-dy2gc
    @RohitKumar-dy2gc ปีที่แล้ว

    u r so dedicated man i wanna be also that much

  • @sourabhchoudhary7289
    @sourabhchoudhary7289 3 ปีที่แล้ว +4

    why can't we put an extra condition for multiply(mid,n)==m then return mid?

    • @SudhanshuKumar-lp1nr
      @SudhanshuKumar-lp1nr 3 ปีที่แล้ว +2

      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()

  • @cpwithsundar
    @cpwithsundar 2 ปีที่แล้ว +1

    There is no match for your hardwork , but we too would work hard even if it doesn't surpasses yours.

  • @mayankkumar4864
    @mayankkumar4864 3 ปีที่แล้ว +1

    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 ?

  • @yeswanthh5068
    @yeswanthh5068 2 ปีที่แล้ว +1

    Thank you sir understood

  • @saurabh44gupta
    @saurabh44gupta 3 ปีที่แล้ว +1

    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 ปีที่แล้ว +7

      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..

  • @shaamidrees6036
    @shaamidrees6036 ปีที่แล้ว

    amazing explaination as always Thanks

  • @antiquefood6284
    @antiquefood6284 3 ปีที่แล้ว +1

    Recently asked in Apple Interview to my experienced brother !!!

  • @jayadubey_22
    @jayadubey_22 3 ปีที่แล้ว

    keep educating bhaiya nicely explained

  • @nileshsinha7869
    @nileshsinha7869 3 ปีที่แล้ว +1

    Great explanation (as always)👌🙌

  • @s.g.prajapati3597
    @s.g.prajapati3597 2 ปีที่แล้ว

    Great content bhaiya! Learned and understood to the fullest!

  • @pawanlok1776
    @pawanlok1776 ปีที่แล้ว

    thank you for making this video ,

  • @thrilokponnuru3131
    @thrilokponnuru3131 ปีที่แล้ว

    Why the search space start from 1?
    What happens when m value is negative

  • @dhruvmittal4992
    @dhruvmittal4992 3 ปีที่แล้ว +1

    why the search space is not 1 to 13 in first example ?

  • @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

  • @tejaswi4650
    @tejaswi4650 3 ปีที่แล้ว +1

    How did you decide we need to stop at 1e - 6?

  • @amitzerikunthe7635
    @amitzerikunthe7635 ปีที่แล้ว

    hats off to ur dedication 🙏

  • @techcode356
    @techcode356 ปีที่แล้ว

    Thank You!

  • @shivansh8006
    @shivansh8006 2 ปีที่แล้ว +1

    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??

  • @radharamamohanakunnattur3035
    @radharamamohanakunnattur3035 ปีที่แล้ว

    Understtood!! and a Big Thanks!!

  • @manavmalhotra8513
    @manavmalhotra8513 2 ปีที่แล้ว +4

    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 9 หลายเดือนก่อน

      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.

  • @surajmaity6194
    @surajmaity6194 2 ปีที่แล้ว

    thank you

  • @abcsumits
    @abcsumits 2 ปีที่แล้ว

    what about interpolation search?

  • @ShwetaSingh-qw7mo
    @ShwetaSingh-qw7mo 2 ปีที่แล้ว

    great explanation

  • @prateekverma9166
    @prateekverma9166 3 ปีที่แล้ว +2

    Can anybody tell why we haven`t used low=mid+1 or high=mid-1

    • @takeUforward
      @takeUforward  3 ปีที่แล้ว +3

      Because we want annswer in terms of decimal places, doing a -1 will not help :)

    • @prateekverma9166
      @prateekverma9166 3 ปีที่แล้ว

      @@takeUforward Got it ,thanks Striver

  • @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

  • @lakshmiprasanna7058
    @lakshmiprasanna7058 ปีที่แล้ว

    Understood 💯💯💯

  • @aandz6194
    @aandz6194 2 ปีที่แล้ว

    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.....?

  • @aaaa-pr2dd
    @aaaa-pr2dd 3 ปีที่แล้ว

    😳awesome dada, didn't knew to use binary search this way

  • @bhaveshkumar6842
    @bhaveshkumar6842 2 ปีที่แล้ว +1

    Thank you, Striver!!!!!!!!!!

  • @darkamv-x5822
    @darkamv-x5822 2 ปีที่แล้ว

    You are best

  • @ankitkholiya4305
    @ankitkholiya4305 ปีที่แล้ว

    Thanks bro you are great

  • @ayushbisht2689
    @ayushbisht2689 3 ปีที่แล้ว

    Best explanation.

  • @sonushaw3715
    @sonushaw3715 2 ปีที่แล้ว

    Thank you bhaia love you❤

  • @son_of_a_dentist8107
    @son_of_a_dentist8107 ปีที่แล้ว

    very good video

  • @codeguy21
    @codeguy21 3 ปีที่แล้ว +1

    @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

  • @VishalKumar-xm5ue
    @VishalKumar-xm5ue 2 ปีที่แล้ว +1

    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...

  • @jotaroackerman3447
    @jotaroackerman3447 3 ปีที่แล้ว

    amazing video bhaiya

  • @amitkumaryadav7240
    @amitkumaryadav7240 2 ปีที่แล้ว

    Can anyone explain why time complexity is not n×log(m)

  • @shubhamnalawade1606
    @shubhamnalawade1606 3 ปีที่แล้ว

    Outstanding explanation

    • @shubhamnalawade1606
      @shubhamnalawade1606 3 ปีที่แล้ว

      Could you please make videos solving coding problems in contests

  • @kushalchakraborty8615
    @kushalchakraborty8615 ปีที่แล้ว

    Great content

  • @antrapurohit8010
    @antrapurohit8010 2 ปีที่แล้ว

    great solution. but it is not accepted in coding ninja platform

  • @s_3095
    @s_3095 ปีที่แล้ว

    thanks bhai striver

  • @abhinanda7049
    @abhinanda7049 9 หลายเดือนก่อน

    understood

  • @ayushkapri9082
    @ayushkapri9082 3 ปีที่แล้ว +1

    why search space is not reduced to mid - 1, why just mid ??

    • @takeUforward
      @takeUforward  3 ปีที่แล้ว +1

      Because mid could be your answer..

  • @shubhamsinha2362
    @shubhamsinha2362 ปีที่แล้ว

    Why can't we just use the Pow function?

  • @yashwagh83
    @yashwagh83 2 ปีที่แล้ว

    Thanks for your efforts and sleepless nights. You are doing god's work Raj.

  • @funenjoynilaypatel4553
    @funenjoynilaypatel4553 2 ปีที่แล้ว +1

    9 march 2022 9:39 pm

  • @sarthaksinghrajput5462
    @sarthaksinghrajput5462 3 ปีที่แล้ว

    Thank you Bhaiya!

  • @AbhishekKumar-td5zu
    @AbhishekKumar-td5zu 3 ปีที่แล้ว

    tqsm bhaiya

  • @Sksahu_123
    @Sksahu_123 3 ปีที่แล้ว

    Bhaiya jisme setprecision use karte so wale bhi question upload kardo

  • @abhijeetkumar2970
    @abhijeetkumar2970 ปีที่แล้ว

    Understood

  • @ahishnar1568
    @ahishnar1568 3 ปีที่แล้ว

    Really nice explanation striver.

    • @takeUforward
      @takeUforward  3 ปีที่แล้ว +5

      Pura th dekh lo 😂

    • @ishankbansal9239
      @ishankbansal9239 3 ปีที่แล้ว

      @@takeUforward 🤣🤣🤣🤣🤣🤣🤣🤣

    • @ahishnar1568
      @ahishnar1568 3 ปีที่แล้ว +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💯💯

  • @willturner3440
    @willturner3440 3 ปีที่แล้ว

    Sde sheet become interesting day by day

  • @amaanakhter2217
    @amaanakhter2217 2 ปีที่แล้ว

    code zen pur submit he nhai ho raha

  • @yogeshyts
    @yogeshyts 2 ปีที่แล้ว

    double t=1.000000/n;
    double ans=pow(m,t);
    return ans;

  • @bilalkhan2298
    @bilalkhan2298 ปีที่แล้ว

    Can't thank u enough

  • @caushic8881
    @caushic8881 3 ปีที่แล้ว

    ay yo mad content G
    Big up your thing💯💯

  • @ng3w462
    @ng3w462 3 ปีที่แล้ว +2

    Can I achieve command on full DSA in 6 months?

    • @RamKumar-kz8gg
      @RamKumar-kz8gg 3 ปีที่แล้ว +2

      you can do whatever you can imagine

    • @ng3w462
      @ng3w462 3 ปีที่แล้ว +1

      @@RamKumar-kz8gg thanks for the motivation bro...🤟

  • @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 ปีที่แล้ว +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 :)

  • @navendraagrawal
    @navendraagrawal 2 ปีที่แล้ว +1

    this solution giving wa in code studio
    what can i change in this?

    • @juzarantri864
      @juzarantri864 2 ปีที่แล้ว +1

      bro did u find any needful?

    • @navendraagrawal
      @navendraagrawal 2 ปีที่แล้ว

      @@juzarantri864 no I just did pow(m,1.0/n)

  • @AnkitMishra-mz4xt
    @AnkitMishra-mz4xt 3 ปีที่แล้ว

    Eagerly wanting

  • @umarqureshi8499
    @umarqureshi8499 2 ปีที่แล้ว

    so good bhai!!!!!!!!!

  • @AbhishekPandey-lg8zd
    @AbhishekPandey-lg8zd 3 ปีที่แล้ว

    Love your effort :)

  • @gymGirlForLyf
    @gymGirlForLyf 3 ปีที่แล้ว

    Where can we pratice more binary search problems ?🙄
    If on any platform what keyword do I have to use to get those questions?🙄

    • @nideeshn6293
      @nideeshn6293 3 ปีที่แล้ว

      Lol use binary search as keyword

  • @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

  • @irrompible4588
    @irrompible4588 3 ปีที่แล้ว

    what if i need the ans upto 2 decimal place should the difference will be e^-2?
    if possible reply ..

    • @takeUforward
      @takeUforward  3 ปีที่แล้ว

      Yeah bro.. -3, so that the first 2 decimal places are correct

    • @irrompible4588
      @irrompible4588 3 ปีที่แล้ว

      @@takeUforward okay..thank you..

    • @sheffybansal3771
      @sheffybansal3771 ปีที่แล้ว

      @@takeUforward if it's upto 2 decimal places, if high = 0.223 and low = 0.222, we should stop the search as difference is 1e-2. Why are we looking upto 1e-3? Please explain