BS-11. Find the Nth root of an Integer

Поделиться
HTML-код
  • Опубликовано: 2 окт 2024
  • 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

Комментарии • 215

  • @HarshKumar-ip5nr
    @HarshKumar-ip5nr Год назад +26

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

  • @andrewbeef8758
    @andrewbeef8758 Год назад +78

    this is a hidden gem playlist in entire youtube

  • @AtulKumar-c4x7l
    @AtulKumar-c4x7l Год назад +2

    understood
    Thank you striver for such an amazing explanation.

  • @KeepCoding69
    @KeepCoding69 16 дней назад +2

    It's better to keep high as m/n. We can reduce the number of checks using this.

  • @HardikDoshi-ml8ii
    @HardikDoshi-ml8ii 3 месяца назад +3

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

  • @girikgarg8
    @girikgarg8 Год назад +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;
    }

  • @joeljacob4685
    @joeljacob4685 Год назад +7

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

  • @nehathakur40
    @nehathakur40 Год назад +10

    The most simplified explanation one could ever get!

  • @srilathareddy9450
    @srilathareddy9450 6 месяцев назад +1

    wow explanation

  • @graviton001
    @graviton001 2 месяца назад

    Solved this too myself thnx ❤

  • @kunalsingh8796
    @kunalsingh8796 Год назад

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

  • @karthik-varma-1579
    @karthik-varma-1579 Месяц назад +1

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

  • @dank7044
    @dank7044 10 месяцев назад +1

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

  • @savitore
    @savitore 6 месяцев назад +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)?

  • @yashaggarwal6013
    @yashaggarwal6013 7 месяцев назад +1

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

    • @RAHULMAHESHWARIdexter
      @RAHULMAHESHWARIdexter 4 месяца назад +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)

  • @ashishbm3565
    @ashishbm3565 10 месяцев назад

    understood

  • @rupalikumari8829
    @rupalikumari8829 2 месяца назад

    Understood:)

  • @mcbotface
    @mcbotface Год назад +2

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

  • @shoaibaltaf1128
    @shoaibaltaf1128 Месяц назад

    int NthRoot(int n, int m) {
    int low=1;
    int high=m;
    int mid;
    while(lowm)high=mid-1;
    else low=mid+1;
    }
    return -1;
    }
    this code got submitted at once without any overflow

  • @ashtonronald
    @ashtonronald 6 месяцев назад

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

  • @fenilpatel6238
    @fenilpatel6238 3 месяца назад

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

  • @cinime
    @cinime Год назад +3

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

  • @NitinKumar-wm2dg
    @NitinKumar-wm2dg Год назад +2

    understood bhaiya, thank you for this tutorial

  • @mangeshtale2818
    @mangeshtale2818 12 дней назад

    int NthRoot(int n, int m)
    {
    // Code here.
    int low=1;
    int high =m;
    while(low m){
    high = mid-1;
    }else{
    low = mid+1;
    }
    }
    return -1;
    }
    T.C = O(logm * logn)
    logm (for binary search) and logn(for calculations of power)
    so isn't this better??

  • @mrsttechno5974
    @mrsttechno5974 Год назад +3

    Your teaching style is awesome, I understand everything in detail

  • @harshilkaria7420
    @harshilkaria7420 14 дней назад

    Can anyone please clear my doubt
    what if in if() condtion in while loop I directly calculate power of mid like this if(pow(mid,n)==m)
    will this genrate overflow ?

  • @AtulKumar-c4x7l
    @AtulKumar-c4x7l Год назад +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 Год назад +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

  • @shalini_msk
    @shalini_msk 11 дней назад

    int NthRoot(int n, int m)
    {
    long long l = 1, r = m;
    if(n == 1) return m;
    while(l

  • @navinvenkat3404
    @navinvenkat3404 2 месяца назад

    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 ?

  • @NARUTOUZUMAKI-bk4nx
    @NARUTOUZUMAKI-bk4nx 7 месяцев назад

    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

  • @hareshnayak7302
    @hareshnayak7302 6 месяцев назад +1

    Understood,Thanks striver for this amazing video.

  • @sahadevnai8040
    @sahadevnai8040 2 месяца назад

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

  • @bharatmehta30
    @bharatmehta30 Год назад +2

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

  • @harshmagarwal3196
    @harshmagarwal3196 4 месяца назад +3

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

    • @VarshaSingh-hi2sb
      @VarshaSingh-hi2sb Месяц назад +1

      What will be the time complexity ? will it be nlogm where m is the number whose root needs to be calculated and it the given power? log m for BS and multiply it with n foe calculate power of every number am I right ?

    • @mahidhruv7
      @mahidhruv7 Месяц назад +2

      @@VarshaSingh-hi2sb Yes correct, complexity will be O(n log (m))

    • @rishabhgupta1222
      @rishabhgupta1222 19 дней назад

      @@VarshaSingh-hi2sb it feels lyk u are scolding 😅

  • @ramandeepsingh9294
    @ramandeepsingh9294 Месяц назад +1

    understood

  • @juhichoudhary9281
    @juhichoudhary9281 9 месяцев назад +1

    Understood

  • @AayushRana-kl4lj
    @AayushRana-kl4lj Год назад +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.

  • @pratikshadhole6694
    @pratikshadhole6694 Год назад +2

    understood

  • @aastharathore3734
    @aastharathore3734 2 месяца назад

    understood

    • @captain-ne8qy
      @captain-ne8qy 2 месяца назад

      Time complexity for the optimal approach?....while handling overflow condition M

  • @kushagramishra5638
    @kushagramishra5638 Год назад +2

    UnderStood!

  • @AbhishekKumar-nz9dn
    @AbhishekKumar-nz9dn 11 месяцев назад +1

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

  • @ShubhanshuG007
    @ShubhanshuG007 Год назад

    Understood

  • @suryanshsoni3420
    @suryanshsoni3420 Год назад +2

    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 7 месяцев назад

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

    • @suryanshsoni3420
      @suryanshsoni3420 7 месяцев назад

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

  • @purushottam108
    @purushottam108 3 месяца назад

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

  • @random_akki
    @random_akki Год назад +2

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

    • @Sankhamit
      @Sankhamit Год назад

      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 Год назад

      @@Sankhamit leetcode accepted the solution using pow

  • @AnmolGupta-oj4lm
    @AnmolGupta-oj4lm Год назад +2

    Understood Very Well!

  • @manavsingh5919
    @manavsingh5919 Год назад +1

    Thank you Striver....Understood everything🙂

  • @srilathareddy9450
    @srilathareddy9450 Год назад +1

    Understood

  • @anvvimalik5579
    @anvvimalik5579 2 месяца назад

    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 Месяц назад

      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

  • @johndurai2226
    @johndurai2226 Год назад +1

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

  • @TrinmoyDutta
    @TrinmoyDutta 6 месяцев назад

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

  • @omkumarsingh885
    @omkumarsingh885 Год назад +1

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

    • @AbhishekKumar-nz9dn
      @AbhishekKumar-nz9dn 11 месяцев назад

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

  • @CodewithKing360
    @CodewithKing360 Месяц назад

    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😄

  • @shaswatshah4920
    @shaswatshah4920 4 месяца назад

    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

  • @dreamyme543
    @dreamyme543 Год назад +1

    understood🙃

  • @PriyaSharma-v8u
    @PriyaSharma-v8u Год назад

    understood

  • @omkarsawant9267
    @omkarsawant9267 7 месяцев назад +3

    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.

  • @venkateshcharyakaram6547
    @venkateshcharyakaram6547 10 дней назад

    explaining the concepts excellent

  • @viveksoni6823
    @viveksoni6823 7 месяцев назад

    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 7 месяцев назад +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 7 месяцев назад

      @@HimanshuYadav-ry8tk thanks a lot

  • @srijall
    @srijall Год назад +2

    Why 69 as an example 😅?

  • @23cash86
    @23cash86 Год назад +1

    ++thanks

  • @DevanshiKapla
    @DevanshiKapla Год назад +1

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

  • @Manishgupta200
    @Manishgupta200 Год назад

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

  • @ashutoshranjan4644
    @ashutoshranjan4644 Год назад

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

  • @priyanshuchoudhary246
    @priyanshuchoudhary246 Год назад +1

    Amazing Playlists

  • @shashankadiga7796
    @shashankadiga7796 8 месяцев назад

    does anyone know where to use low

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

  • @SandyCr7
    @SandyCr7 8 дней назад

    understood

  • @esmamanyt7048
    @esmamanyt7048 11 месяцев назад

    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

  • @captainakash8914
    @captainakash8914 4 месяца назад

    thanks you striver for.... easy explanation

  • @oyeesharme
    @oyeesharme 2 месяца назад

    understood bhaiya

  • @akshatsamdani
    @akshatsamdani Год назад

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

  • @RaunitJaiswal-s9v
    @RaunitJaiswal-s9v 16 дней назад

    Last for today 😮

  • @harshilpatel3205
    @harshilpatel3205 Месяц назад

    Understood 🙏🏻

  • @nayankhuman1043
    @nayankhuman1043 2 месяца назад

    Understood :)

  • @suryasaimaheswar8636
    @suryasaimaheswar8636 3 месяца назад

    understood:)

  • @rajatshukla2605
    @rajatshukla2605 5 дней назад

    understood!

  • @sanketatmaram
    @sanketatmaram Месяц назад

    understood!

  • @codeman3828
    @codeman3828 Месяц назад

    Understood.

  • @DeadPoolx1712
    @DeadPoolx1712 Месяц назад

    UNDERSTOOD;

  • @nm.tech1001
    @nm.tech1001 Месяц назад

    Understood😊

  • @manovmandal3511
    @manovmandal3511 2 месяца назад

    Understood!

  • @nishant4595
    @nishant4595 2 месяца назад

    understood!

  • @nowornever8552
    @nowornever8552 20 дней назад

    Understoood

  • @rishijha3201
    @rishijha3201 7 дней назад

    understood

  • @maccode07
    @maccode07 17 дней назад

    UNDERSTOOD

  • @Try_me_
    @Try_me_ 22 дня назад

    understood

  • @dronnema203
    @dronnema203 Месяц назад

    understood

  • @vg0410
    @vg0410 Месяц назад

    understood

  • @anshsaxena7297
    @anshsaxena7297 Месяц назад

    UnderStood

  • @padduchennamsetti6516
    @padduchennamsetti6516 Месяц назад

    understood

  • @ANaskar-vw4tn
    @ANaskar-vw4tn Месяц назад

    Understood

  • @RishabhKumar0094
    @RishabhKumar0094 2 месяца назад

    understood

  • @Harsh-jc2bz
    @Harsh-jc2bz 2 месяца назад

    understood

  • @nishtha283
    @nishtha283 2 месяца назад

    Understood

  • @Sanket4267
    @Sanket4267 8 месяцев назад

    understood

  • @ganeshvhatkar9253
    @ganeshvhatkar9253 8 месяцев назад

    understood

  • @udatyadeb101
    @udatyadeb101 8 месяцев назад

    understood

  • @dewanandkumar8589
    @dewanandkumar8589 9 месяцев назад

    Understood

  • @firebout7675
    @firebout7675 9 месяцев назад

    understood