BS-11. Find the Nth root of an Integer

Поділитися
Вставка
  • Опубліковано 20 чер 2023
  • Problem Link: bit.ly/3oWhSkW
    Notes/C++/Java/Python codes: takeuforward.org/data-structu...
    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

КОМЕНТАРІ • 167

  • @andrewbeef8758
    @andrewbeef8758 11 місяців тому +49

    this is a hidden gem playlist in entire youtube

  • @HarshKumar-ip5nr
    @HarshKumar-ip5nr 11 місяців тому +20

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

  • @nehathakur40
    @nehathakur40 11 місяців тому +4

    The most simplified explanation one could ever get!

  • @cinime
    @cinime Рік тому

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

  • @mrsttechno5974
    @mrsttechno5974 8 місяців тому +1

    Your teaching style is awesome, I understand everything in detail

  • @bharatmehta30
    @bharatmehta30 10 місяців тому

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

  • @user-ti3bd8mp1w
    @user-ti3bd8mp1w 11 місяців тому

    understood
    Thank you striver for such an amazing explanation.

  • @manavsingh5919
    @manavsingh5919 9 місяців тому

    Thank you Striver....Understood everything🙂

  • @joeljacob4685
    @joeljacob4685 8 місяців тому +3

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

  • @johndurai2226
    @johndurai2226 Рік тому

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

  • @hareshnayak7302
    @hareshnayak7302 2 місяці тому

    Understood,Thanks striver for this amazing video.

  • @AnmolGupta-oj4lm
    @AnmolGupta-oj4lm 10 місяців тому

    Understood Very Well!

  • @priyanshuchoudhary246
    @priyanshuchoudhary246 9 місяців тому

    Amazing Playlists

  • @NitinKumar-wm2dg
    @NitinKumar-wm2dg 11 місяців тому

    understood bhaiya, thank you for this tutorial

  • @VivekSharma-sk3vp
    @VivekSharma-sk3vp 2 місяці тому

    understood!! nicely

  • @NazeerBashaShaik
    @NazeerBashaShaik 2 місяці тому

    Understood, thank you.

  • @AayushRana-kl4lj
    @AayushRana-kl4lj Рік тому +2

    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.

  • @captainakash8914
    @captainakash8914 Місяць тому

    thanks you striver for.... easy explanation

  • @harshmagarwal3196
    @harshmagarwal3196 26 днів тому

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

  • @justcodeitbro1312
    @justcodeitbro1312 10 місяців тому

    Damn bhai what a great explanation thanks alot

  • @kunalsingh8796
    @kunalsingh8796 9 місяців тому

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

  • @lakshmiprasanna7058
    @lakshmiprasanna7058 11 місяців тому

    Understood 💯💯💯

  • @YourCodeVerse
    @YourCodeVerse 6 місяців тому

    Understood✅🔥🔥

  • @shaiksoofi3741
    @shaiksoofi3741 3 місяці тому

    understood Thank you

  • @ayushirastogi9725
    @ayushirastogi9725 11 місяців тому

    yep understood!!

  • @gopimalisetti7900
    @gopimalisetti7900 Рік тому

    Understood! 😀

  • @girikgarg8
    @girikgarg8 9 місяців тому +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;
    }

  • @purushottam108
    @purushottam108 7 днів тому

    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.🤩🤩🤩🤩🤩🤩❤❤😥😥

  • @diponchetia9381
    @diponchetia9381 Місяць тому

    understood clearly

  • @senseiAree
    @senseiAree 8 місяців тому

    Understood ❤

  • @harsh_parmar601
    @harsh_parmar601 4 місяці тому

    Understood 🎉

  • @user-is6ky7pp2n
    @user-is6ky7pp2n 16 днів тому

    Understood !! 😎😎

  • @UECAbhishekKumar
    @UECAbhishekKumar 4 місяці тому

    Understood❤

  • @humanity7880
    @humanity7880 10 місяців тому

    understood!

  • @TON-108
    @TON-108 7 місяців тому

    Understood!

  • @thatsathishkumar
    @thatsathishkumar Рік тому

    Nice bhai

  • @ArpanChakraborty-do6yz
    @ArpanChakraborty-do6yz 4 місяці тому

    just op❤‍🔥💯

  • @mcbotface
    @mcbotface Рік тому +2

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

  • @user-nk1mb5fy7j
    @user-nk1mb5fy7j Рік тому

    UNDERSTOOD

  • @user-uv5or5bm2c
    @user-uv5or5bm2c 21 день тому

    Understood!!

  • @user-tk2vg5jt3l
    @user-tk2vg5jt3l 4 місяці тому

    Thank you Bhaiya

  • @srilathareddy9450
    @srilathareddy9450 2 місяці тому

    wow explanation

  • @dank7044
    @dank7044 7 місяців тому +1

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

  • @googleit2490
    @googleit2490 Рік тому

    Understood :)

  • @utsavseth6573
    @utsavseth6573 11 місяців тому

    Lovely.

  • @padmavatishah9922
    @padmavatishah9922 3 місяці тому

    Understood sir

  • @AmanKumar-qz4jz
    @AmanKumar-qz4jz 9 місяців тому

    thanks

  • @amitranjeetjha1240
    @amitranjeetjha1240 Рік тому

    Understood

  • @pratikshadhole6694
    @pratikshadhole6694 Рік тому +1

    understood

  • @shashwat_harsh
    @shashwat_harsh Рік тому

    understood.

  • @rushithagudipudi7742
    @rushithagudipudi7742 2 місяці тому

    Understand

  • @dreamyme543
    @dreamyme543 8 місяців тому

    understood🙃

  • @HardikDoshi-ml8ii
    @HardikDoshi-ml8ii 7 днів тому +1

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

  • @sarangkumarsingh7901
    @sarangkumarsingh7901 Місяць тому

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

  • @ashutoshranjan4644
    @ashutoshranjan4644 9 місяців тому

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

  • @23cash86
    @23cash86 Рік тому

    ++thanks

  • @suryanshsoni3420
    @suryanshsoni3420 Рік тому +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.

    • @yashaggarwal6013
      @yashaggarwal6013 4 місяці тому

      what is the logic behind m / (Math.pow(mid, n - 1) ?

    • @suryanshsoni3420
      @suryanshsoni3420 4 місяці тому

      @@yashaggarwal6013 mid * mid**(n-1) = mid**n

  • @user-rf7ri9pw9p
    @user-rf7ri9pw9p Рік тому +1

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

  • @innocentyadavabhay5297
    @innocentyadavabhay5297 Рік тому

  • @esmamanyt7048
    @esmamanyt7048 7 місяців тому

    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

  • @shaswatshah4920
    @shaswatshah4920 15 днів тому

    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

  • @akshatsamdani
    @akshatsamdani 11 місяців тому

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

  • @savitore
    @savitore 2 місяці тому

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

  • @modiji8706
    @modiji8706 7 місяців тому

    able to write this code myself

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Рік тому

    Understood please solve some hard problems

  • @prthms_
    @prthms_ 4 місяці тому

    i got it

  • @omkarsawant9267
    @omkarsawant9267 3 місяці тому +1

    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.

  • @user-ti3bd8mp1w
    @user-ti3bd8mp1w 11 місяців тому +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 9 місяців тому +1

      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

  • @random_akki
    @random_akki Рік тому +2

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

    • @user-hq7yd4wz4y
      @user-hq7yd4wz4y 11 місяців тому

      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 11 місяців тому

      @@user-hq7yd4wz4y leetcode accepted the solution using pow

  • @user-jg7eb3xt9q
    @user-jg7eb3xt9q 2 місяці тому

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

  • @shashankadiga7796
    @shashankadiga7796 5 місяців тому

    does anyone know where to use low

  • @yashaggarwal6013
    @yashaggarwal6013 4 місяці тому +1

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

    • @RAHULMAHESHWARIdexter
      @RAHULMAHESHWARIdexter Місяць тому +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)

  • @zebra-er6xc
    @zebra-er6xc 7 місяців тому

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

    • @dhruvchopra26
      @dhruvchopra26 5 місяців тому +1

      Yes he has corrected it in the video also

  • @sharubs9907
    @sharubs9907 6 місяців тому

    What is square root of -6

  • @priyaanshusoni
    @priyaanshusoni Місяць тому

    why're we returning 2 ?

  • @kushagramishra5638
    @kushagramishra5638 Рік тому +2

    UnderStood!

  • @raghavkansal3765
    @raghavkansal3765 11 місяців тому

    "understood"

  • @akshatgosain3831
    @akshatgosain3831 Рік тому

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

  • @kishan.17
    @kishan.17 Рік тому

    1st view❤❤❤❤

  • @MischievousSoul
    @MischievousSoul 11 місяців тому

    Cfbr

  • @anubhav_gupta_
    @anubhav_gupta_ 9 місяців тому

    US✅

  • @tdeep01
    @tdeep01 10 місяців тому

    god

  • @Raj10185
    @Raj10185 Рік тому

    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

  • @Manishgupta200
    @Manishgupta200 10 місяців тому

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

  • @viveksoni6823
    @viveksoni6823 3 місяці тому

    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 3 місяці тому +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 3 місяці тому

      @@HimanshuYadav-ry8tk thanks a lot

  • @satishreddy1497
    @satishreddy1497 Рік тому

    First viewer

  • @fenilpatel6238
    @fenilpatel6238 10 днів тому

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

  • @srijall
    @srijall Рік тому +2

    Why 69 as an example 😅?

  • @aj9706
    @aj9706 2 місяці тому

    US

  • @omkumarsingh885
    @omkumarsingh885 11 місяців тому +1

    engineers and their obsession with the number 69😂😂😂😂😂😂😂😂

    • @AbhishekKumar-nz9dn
      @AbhishekKumar-nz9dn 8 місяців тому

      hahahahahah i was looking for this comment to see wheather someone else has also noticed this or its only me 🤣

  • @RahulKumar-mi1sj
    @RahulKumar-mi1sj Рік тому

    1st view

  • @rahulhembram4519
    @rahulhembram4519 10 місяців тому

    underStood

  • @debjitmisra8223
    @debjitmisra8223 Рік тому

    1st

  • @user-sm7zo5zd9t
    @user-sm7zo5zd9t 3 місяці тому

    UNderstood

  • @cenacr007
    @cenacr007 8 місяців тому

    us

  • @dipanshuraj7868
    @dipanshuraj7868 Місяць тому

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

  • @NARUTOUZUMAKI-bk4nx
    @NARUTOUZUMAKI-bk4nx 4 місяці тому

    Can anyone explain why this code is not working
    class Solution{
    public:
    int power(int i, int n, int m){
    long long res = 1;
    while(n){
    if(n & 1) res = res * i;
    if(res > m) return 2;
    i = i * i;
    n >>= 1;
    }
    if(res == m) return 1;
    return 0;
    }
    int NthRoot(int n, int m){
    int l = 1, h = m;
    while(l

  • @AbhishekKumar-nz9dn
    @AbhishekKumar-nz9dn 8 місяців тому +1

    kch number na aaye dimag me , 69 jroor ata h 😆

  • @ashtonronald
    @ashtonronald 2 місяці тому

    #include
    using namespace std;
    using ll = long long;
    ll binExp(ll a, ll b, ll m){
    // implementation of binary exponentiation without modulus
    ll ans = 1;
    while(b){
    if (b % 2 == 1){
    if (a 0) ans *= a; // ans may exceed m
    else return m+1;
    }
    a *= a; // a may exceed m
    b /= 2;
    if (ans > m || ans < 0) return m+1; // may end up negative in case of overflow (not allowed constraints)
    }
    return ans;
    }
    int NthRoot(int n, int m) {
    if (m == 1) return 1;
    int lo = 1; int hi = m;
    while(lo m) hi = mi-1;
    else lo = mi+1;
    }
    return -1;
    }
    The implementation of the binary exponentiation without the help of modulo was one hell of a ride. Thanks for the insight.

  • @senseiAree
    @senseiAree 8 місяців тому

    Understood ❤