Video není dostupné.
Omlouváme se.

BS-11. Find the Nth root of an Integer

Sdílet
Vložit
  • čas přidán 20. 06. 2023
  • Problem Link: bit.ly/3oWhSkW
    Notes/C++/Java/Python codes: takeuforward.o...
    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/take...
    0:00 Introduction of Course

Komentáře • 189

  • @andrewbeef8758
    @andrewbeef8758 Před rokem +68

    this is a hidden gem playlist in entire youtube

  • @HarshKumar-ip5nr
    @HarshKumar-ip5nr Před rokem +24

    The edge case of overflow was amazing. Thank you for such amazing content. Consider completing the series we are eagerly waiting.

  • @nehathakur40
    @nehathakur40 Před rokem +9

    The most simplified explanation one could ever get!

  • @joeljacob4685
    @joeljacob4685 Před 10 měsíci +7

    Handling the overflow case was amazing !! I didn't got that one

  • @harshmagarwal3196
    @harshmagarwal3196 Před 2 měsíci +2

    Sometimes I feel he is scolding me. But honestly this is one of the best playlist available on YT.

  • @mrsttechno5974
    @mrsttechno5974 Před 10 měsíci +3

    Your teaching style is awesome, I understand everything in detail

  • @omkarsawant9267
    @omkarsawant9267 Před 5 měsíci +2

    Edge case explained when mid^n > m then overfllow occurs
    int func(int mid, int n, int m)
    {
    long long ans = 1;
    for (int i = 1; i m)
    {
    return 2;
    }
    }
    if (ans == m)
    {
    return 1;
    } // return 1 if ans == m
    return 0; // return 0 if ans < m
    }
    For example, let's say mid = 3, n = 2, and m = 8. During the loop, the value of ans will be calculated as follows:
    ans = 1 * 3 = 3 (after the first iteration)
    ans = 3 * 3 = 9 (after the second iteration)
    At this point, ans (which is now 9) is greater than m (which is 8), and the function will return 2, indicating that 3^2 is greater than 8.

  • @bharatmehta30
    @bharatmehta30 Před rokem +2

    Was missing the overflow case. Thanks to this amazing video.

  • @cinime
    @cinime Před rokem +3

    Understood! Super amazing explanation as always, thank you so so much for your effort!!

  • @user-ti3bd8mp1w
    @user-ti3bd8mp1w Před rokem +2

    understood
    Thank you striver for such an amazing explanation.

  • @hareshnayak7302
    @hareshnayak7302 Před 4 měsíci +1

    Understood,Thanks striver for this amazing video.

  • @dank7044
    @dank7044 Před 9 měsíci +1

    suppose M=10^63-1;( max value of long long), and mid=10^63/2, now is mid

  • @NitinKumar-wm2dg
    @NitinKumar-wm2dg Před rokem +2

    understood bhaiya, thank you for this tutorial

  • @girikgarg8
    @girikgarg8 Před 11 měsíci +3

    Done, Here's my approach to this question:
    long long binPower(int a,int b,int m){
    long long ans=1;
    long long temp=a;
    while (b){
    if (temp>m || ans*temp>m) return INT_MAX; // so that high moves to mid-1
    if (b&1){
    ans=1LL*ans*temp;
    }
    temp=1LL*temp*temp;
    b>>=1;
    }
    return ans;
    }
    int NthRoot(int n, int m) {
    int low=1;
    int high=m;
    int ans=0;
    while (lowm) high=mid-1;
    else low=mid+1;
    }
    return -1;
    }

  • @johndurai2226
    @johndurai2226 Před rokem +1

    I am your big fan because of your videos are well and explanation also

  • @manavsingh5919
    @manavsingh5919 Před 11 měsíci +1

    Thank you Striver....Understood everything🙂

  • @AnmolGupta-oj4lm
    @AnmolGupta-oj4lm Před rokem +2

    Understood Very Well!

  • @AayushRana-kl4lj
    @AayushRana-kl4lj Před rokem +5

    Root of any even num is always even and same goes with odd too. We should apply this concept too to reduce a bit of time complicity.

  • @purushottam108
    @purushottam108 Před 2 měsíci

    to understand this video i lean power exp it take me 2 hrs to learn because of -ve power in leet code ,and at last striver solve this porblem by simple for loop.🤩🤩🤩🤩🤩🤩❤❤😥😥

  • @srilathareddy9450
    @srilathareddy9450 Před 4 měsíci +1

    wow explanation

  • @priyanshuchoudhary246
    @priyanshuchoudhary246 Před 11 měsíci +1

    Amazing Playlists

  • @graviton001
    @graviton001 Před 14 dny

    Solved this too myself thnx ❤

  • @kunalsingh8796
    @kunalsingh8796 Před 11 měsíci

    consistency to gave such awesome videos makes u a good youtuber ...keep doing sir

  • @srilathareddy9450
    @srilathareddy9450 Před rokem +1

    Understood

  • @captainakash8914
    @captainakash8914 Před 3 měsíci

    thanks you striver for.... easy explanation

  • @nm.tech1001
    @nm.tech1001 Před 7 dny

    Understood😊

  • @oyeesharme
    @oyeesharme Před 11 dny

    understood bhaiya

  • @pratikshadhole6694
    @pratikshadhole6694 Před rokem +2

    understood

  • @suryanshsoni3420
    @suryanshsoni3420 Před rokem +1

    long start = 1;
    long end = m;
    while (start m / (Math.pow(mid, n - 1))) {
    end = mid - 1;
    } else if (mid < m / (Math.pow(mid, n - 1))) {
    start = mid + 1;
    } else {
    return (int) mid;
    }
    }
    return -1;
    This also works.

  • @mcbotface
    @mcbotface Před rokem +2

    Can you share the code where you modify exponentiation function to handle overflow situation instead of naive multiplication function?

  • @savitore
    @savitore Před 4 měsíci +1

    when you are modifying code for overflow, the T.C got changed to O(n*logm). How to do it in O(logn logm)?

  • @dreamyme543
    @dreamyme543 Před 10 měsíci +1

    understood🙃

  • @HardikDoshi-ml8ii
    @HardikDoshi-ml8ii Před 2 měsíci +1

    can we use power func --- pow(mid,n) ??? instead of writing different function??

  • @user-ti3bd8mp1w
    @user-ti3bd8mp1w Před rokem +2

    Striver can u please explain how can I do with binary exponentiation.....i tried still i am not able to figure it out....
    i used following code:
    long long ans=1;
    while(n>0)
    {
    if(n%2==1)
    {
    ans=ans*mid
    n=n-1;
    }
    else{
    mid=mid*mid;
    if(mid>m)
    return 2;
    n=n/2;
    }
    }
    if(ans==m) return 1;
    return 0;

    • @rishabhagarwal6057
      @rishabhagarwal6057 Před 11 měsíci +2

      This is using binary exponentiation. TC = O(log(m)*log(n)
      class Solution{
      public:
      long long fun(long x, long long n, long long m){
      long long ans =1;
      while(n>0){
      if(n%2==1){
      ans=ans*x;
      if(ans>m) return -1;
      n--;
      }
      else{
      x=x*x;
      if(x>m) return -1;
      n/=2;
      }
      }
      }
      int NthRoot(int n, int m)
      {
      long long low =0;
      long long high =m;
      while(low

  • @justcodeitbro1312
    @justcodeitbro1312 Před rokem

    Damn bhai what a great explanation thanks alot

  • @manovmandal3511
    @manovmandal3511 Před měsícem

    Understood!

  • @yashaggarwal6013
    @yashaggarwal6013 Před 5 měsíci +1

    Why can't we use pow(mid,n) to calculate power instead of writing an entire function in C++?

    • @RAHULMAHESHWARIdexter
      @RAHULMAHESHWARIdexter Před 2 měsíci +1

      watch the video from 15:30 till end, it will cause overflow.
      one workaround is to do pow(mid,1/n), take floor of it and check if it is same as the number (i mean to say if its a integer instead of some float value like 3.21 something)

  • @VivekSharma-sk3vp
    @VivekSharma-sk3vp Před 4 měsíci

    understood!! nicely

  • @diponchetia9381
    @diponchetia9381 Před 2 měsíci

    understood clearly

  • @NazeerBashaShaik
    @NazeerBashaShaik Před 4 měsíci

    Understood, thank you.

  • @nishant4595
    @nishant4595 Před měsícem

    understood!

  • @shaswatshah4920
    @shaswatshah4920 Před 2 měsíci

    Can't we use mid**n to check and then increase or decrease the low and high accordingly.
    this way we don't have to use the function and no need of for loop also

  • @user-is6ky7pp2n
    @user-is6ky7pp2n Před 2 měsíci

    Understood !! 😎😎

  • @shaiksoofi3741
    @shaiksoofi3741 Před 5 měsíci

    understood Thank you

  • @CodewithKing360
    @CodewithKing360 Před 7 dny

    bhaiyya in square root question we can minimize some iteration using high=n/2
    a squreRoot of number always lies in 1 to num/2😄

  • @YourCodeVerse
    @YourCodeVerse Před 8 měsíci

    Understood✅🔥🔥

  • @nayankhuman1043
    @nayankhuman1043 Před 29 dny

    Understood :)

  • @ashutoshranjan4644
    @ashutoshranjan4644 Před 11 měsíci

    In python, it doesn't cause any issue with that code. But thanks for that edge case of c++.

  • @random_akki
    @random_akki Před rokem +2

    instead of func can we use pow(mid,n)??

    • @user-hq7yd4wz4y
      @user-hq7yd4wz4y Před rokem

      no at the end of the video bhaiya explained the case of 10 to the power 90 which will overflow so we use the function , if we use pow it will directly give 10 the power 90 which will overflow and we ll not get output

    • @random_akki
      @random_akki Před rokem

      @@user-hq7yd4wz4y leetcode accepted the solution using pow

  • @harsh_parmar601
    @harsh_parmar601 Před 5 měsíci

    Understood 🎉

  • @chiragbansod8252
    @chiragbansod8252 Před 5 měsíci

    UNDERSTOOD

  • @user-rf7ri9pw9p
    @user-rf7ri9pw9p Před rokem +1

    instead of the check func can we use inbuilt power func that will also work

  • @senseiAree
    @senseiAree Před 10 měsíci

    Understood ❤

  • @padmavatishah9922
    @padmavatishah9922 Před 5 měsíci

    Understood sir

  • @user-jg7eb3xt9q
    @user-jg7eb3xt9q Před 4 měsíci

    lets assume we need to find nth root of num, so if we set low=0, high=num/n, then the overflowing edge case can be avoided to some extent, as we know nth root cannot exceed num/n...

  • @lakshmiprasanna7058
    @lakshmiprasanna7058 Před rokem

    Understood 💯💯💯

  • @user-tk2vg5jt3l
    @user-tk2vg5jt3l Před 6 měsíci

    Thank you Bhaiya

  • @sarangkumarsingh7901
    @sarangkumarsingh7901 Před 3 měsíci

    Understood...............

  • @UECAbhishekKumar
    @UECAbhishekKumar Před 6 měsíci

    Understood❤

  • @user-uv5or5bm2c
    @user-uv5or5bm2c Před 2 měsíci

    Understood!!

  • @ayushirastogi9725
    @ayushirastogi9725 Před rokem

    yep understood!!

  • @modiji8706
    @modiji8706 Před 9 měsíci

    able to write this code myself

  • @suryasaimaheswar8636
    @suryasaimaheswar8636 Před měsícem

    understood:)

  • @ArpanChakraborty-do6yz
    @ArpanChakraborty-do6yz Před 6 měsíci

    just op❤‍🔥💯

  • @AmanKumar-qz4jz
    @AmanKumar-qz4jz Před 11 měsíci

    thanks

  • @rushithagudipudi7742
    @rushithagudipudi7742 Před 4 měsíci

    Understand

  • @anvvimalik5579
    @anvvimalik5579 Před 26 dny

    I have a doubt,instead of creating multiplication function,im simply using pow(mid,n) and im not having overflow
    Is this fine?

    • @jatinukey4062
      @jatinukey4062 Před 10 dny

      is it working fine?? cause I was thinking of the same approach. but I think it can also lead to overflow. In which language you were doing this

  • @gopimalisetti7900
    @gopimalisetti7900 Před rokem

    Understood! 😀

  • @thatsathishkumar
    @thatsathishkumar Před rokem

    Nice bhai

  • @esmamanyt7048
    @esmamanyt7048 Před 9 měsíci

    i have a doubt this just failing the test case but not giving any error and if it not giving an error how can we suppose to find out the problem there has to be any trick or tips for this type of edge case

  • @prthms_
    @prthms_ Před 5 měsíci

    i got it

  • @kushagramishra5638
    @kushagramishra5638 Před rokem +2

    UnderStood!

  • @akshatsamdani
    @akshatsamdani Před rokem

    Isnt the time complexity for this code is O(nlogm)

  • @utsavseth6573
    @utsavseth6573 Před rokem

    Lovely.

  • @shashwat_harsh
    @shashwat_harsh Před rokem

    understood.

  • @dayashankarlakhotia4943

    Understood please solve some hard problems

  • @viveksoni6823
    @viveksoni6823 Před 5 měsíci

    why can't i keep like
    for(int i = 1; i m) return 0;
    res = res*mid;
    // if(res > m) return 0;
    }
    It's giving wrong answer.

    • @HimanshuYadav-ry8tk
      @HimanshuYadav-ry8tk Před 5 měsíci +1

      if mid=5 and m = 34 then
      it will go like this
      first iteration -> res(1)>m not true, res=1*5
      second iteration -> res(5)>m not true, res=5*5
      third iteration ->res(25)>m not true, res=5*5*5
      so your if is not working correctly

    • @viveksoni6823
      @viveksoni6823 Před 5 měsíci

      @@HimanshuYadav-ry8tk thanks a lot

  • @shashankadiga7796
    @shashankadiga7796 Před 7 měsíci

    does anyone know where to use low

  • @23cash86
    @23cash86 Před rokem +1

    ++thanks

  • @fenilpatel6238
    @fenilpatel6238 Před 2 měsíci

    #include
    int NthRoot(int n, int m) {
    // Write your code here.
    int low = 1, high = m;
    while(low

  • @zebra-er6xc
    @zebra-er6xc Před 9 měsíci

    at 2:57 time complexity will be O( Nth Root (M)) right?

    • @dhruvchopra26
      @dhruvchopra26 Před 7 měsíci +1

      Yes he has corrected it in the video also

  • @dipanshuraj7868
    @dipanshuraj7868 Před 3 měsíci

    instead of using another function, we have to use the pow(mid, n) function.
    #include
    int NthRoot(int n, int m) {
    // Write your code here.
    for(int i = 1;i m){
    return -1;
    }
    }
    return -1;
    }

  • @navinvenkat3404
    @navinvenkat3404 Před měsícem

    int NthRoot(int n, int m)
    {
    if (m == 0) {
    return 0; // Special case: 0th root of 0 is 0
    }

    int low = 1;
    int high = m;
    int result = -1;

    while (low m) {
    high = mid - 1;
    } else {
    low = mid + 1;
    result = mid; // Keep track of potential result
    }
    }

    return result; // Return the last valid result found
    } whats wrong with code ?

  • @sahadevnai8040
    @sahadevnai8040 Před 18 dny

    public class Solution {
    public static int NthRoot(int n, int m) {
    int low = 1;
    int high = m;
    while (low m) break;
    }
    if (val == m) {
    return mid;
    } else if (val > m) {
    high = mid - 1;
    } else {
    low = mid + 1;
    }
    }
    return -1;
    }
    }

  • @raghavkansal3765
    @raghavkansal3765 Před rokem

    "understood"

  • @MischievousSoul
    @MischievousSoul Před rokem

    Cfbr

  • @Raj10185
    @Raj10185 Před rokem

    my implementation :-
    typedef long long ll;
    ll multiply(ll mid , int n,int m)
    {
    ll ans=1;

    for(int i=1;im)
    {
    //to overcome the overflow bada ho gya to bas break kar do
    break;
    }
    }
    return ans;
    }
    int NthRoot(int n, int m)
    {
    if(n==1)
    return m;
    int low = 1;
    int high = 1e5;

    while(low

  • @sharubs9907
    @sharubs9907 Před 8 měsíci

    What is square root of -6

  • @akshatgosain3831
    @akshatgosain3831 Před rokem

    i used pow function and overflow didnt happen,why??

  • @innocentyadavabhay5297

  • @priyaanshusoni
    @priyaanshusoni Před 3 měsíci

    why're we returning 2 ?

  • @Manishgupta200
    @Manishgupta200 Před rokem

    Dealing with the overflow case is too tricky. That's kind of thing is taughts you only

  • @aj9706
    @aj9706 Před 3 měsíci

    US

  • @anubhav_gupta_
    @anubhav_gupta_ Před 11 měsíci

    US✅

  • @srijall
    @srijall Před rokem +2

    Why 69 as an example 😅?

  • @kishan.17
    @kishan.17 Před rokem

    1st view❤❤❤❤

  • @user-sm7zo5zd9t
    @user-sm7zo5zd9t Před 5 měsíci

    UNderstood

  • @tdeep01
    @tdeep01 Před rokem

    god

  • @rahulhembram4519
    @rahulhembram4519 Před rokem

    underStood

  • @satishreddy1497
    @satishreddy1497 Před rokem

    First viewer