The most simplified explanation one could ever get!
@HarshKumar-ip5nr Жыл бұрын
The edge case of overflow was amazing. Thank you for such amazing content. Consider completing the series we are eagerly waiting.
@mrsttechno5974 Жыл бұрын
Your teaching style is awesome, I understand everything in detail
@joeljacob4685 Жыл бұрын
Handling the overflow case was amazing !! I didn't got that one
@bharatmehta30 Жыл бұрын
Was missing the overflow case. Thanks to this amazing video.
@dank7044 Жыл бұрын
suppose M=10^63-1;( max value of long long), and mid=10^63/2, now is mid
@cinime Жыл бұрын
Understood! Super amazing explanation as always, thank you so so much for your effort!!
@harshmagarwal31967 ай бұрын
Sometimes I feel he is scolding me. But honestly this is one of the best playlist available on YT.
@VarshaSingh-hi2sb4 ай бұрын
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 ?
@mahidhruv74 ай бұрын
@@VarshaSingh-hi2sb Yes correct, complexity will be O(n log (m))
@rishabhgupta12224 ай бұрын
@@VarshaSingh-hi2sb it feels lyk u are scolding 😅
@KrishnaKumar-b4m9pАй бұрын
@@VarshaSingh-hi2sb for calculating power there are 2 approaches with time complexity O(n) and O ( log n). so your overall time complexity will depend on which method u choose. where as for the binary search part time complexity will be O (log m)
@HardikDoshi-ml8ii7 ай бұрын
can we use power func --- pow(mid,n) ??? instead of writing different function??
@Bigg_boss_trollsАй бұрын
our teaching style is awesome
@AtulKumar-c4x7l Жыл бұрын
understood Thank you striver for such an amazing explanation.
@johndurai2226 Жыл бұрын
I am your big fan because of your videos are well and explanation also
@hareshnayak73029 ай бұрын
Understood,Thanks striver for this amazing video.
@manavsingh5919 Жыл бұрын
Thank you Striver....Understood everything🙂
@omkarsawant926710 ай бұрын
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.
@girikgarg8 Жыл бұрын
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; }
@suryanshsoni3420 Жыл бұрын
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.
@yashaggarwal601310 ай бұрын
what is the logic behind m / (Math.pow(mid, n - 1) ?
@suryanshsoni342010 ай бұрын
@@yashaggarwal6013 mid * mid**(n-1) = mid**n
@kunalsingh8796 Жыл бұрын
consistency to gave such awesome videos makes u a good youtuber ...keep doing sir
@DivyamGupta-ly4ds Жыл бұрын
oh i really see that coming....😂😂
@NitinKumar-wm2dg Жыл бұрын
understood bhaiya, thank you for this tutorial
@creepitup13 күн бұрын
here if you dont understand sir solution : int NthRoot(int n, int m) { // Write your code here. int low=1;int high=m; while(lowm){ high=mid-1; } else low=mid+1; } return -1; }
@priyanshuchoudhary246 Жыл бұрын
Amazing Playlists
@AayushRana-kl4lj Жыл бұрын
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.
@AnmolGupta-oj4lm Жыл бұрын
Understood Very Well!
@venkateshcharyakaram65473 ай бұрын
explaining the concepts excellent
@zebra-er6xc Жыл бұрын
at 2:57 time complexity will be O( Nth Root (M)) right?
@dhruvchopra26 Жыл бұрын
Yes he has corrected it in the video also
@purushottam1087 ай бұрын
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.🤩🤩🤩🤩🤩🤩❤❤😥😥
@KrishnaKumar-b4m9pАй бұрын
dont worry that will also be useful. it was asked in meta coding interview to implement pow(x,n) function using log n time complexity
@justcodeitbro1312 Жыл бұрын
Damn bhai what a great explanation thanks alot
@savitore9 ай бұрын
when you are modifying code for overflow, the T.C got changed to O(n*logm). How to do it in O(logn logm)?
@mcbotface Жыл бұрын
Can you share the code where you modify exponentiation function to handle overflow situation instead of naive multiplication function?
@yashaggarwal601310 ай бұрын
Why can't we use pow(mid,n) to calculate power instead of writing an entire function in C++?
@RAHULMAHESHWARIdexter7 ай бұрын
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)
@karthik-varma-15794 ай бұрын
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; } }
@captainakash89148 ай бұрын
thanks you striver for.... easy explanation
@AtulKumar-c4x7l Жыл бұрын
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 Жыл бұрын
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
@shaswatshah49207 ай бұрын
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
@shoaibaltaf11284 ай бұрын
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
@graviton0015 ай бұрын
Solved this too myself thnx ❤
@KarthikNandam-xs4qn2 ай бұрын
understood my g 🔥
@random_akki Жыл бұрын
instead of func can we use pow(mid,n)??
@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 Жыл бұрын
@@Sankhamit leetcode accepted the solution using pow
@ashutoshranjan4644 Жыл бұрын
In python, it doesn't cause any issue with that code. But thanks for that edge case of c++.
@TrinmoyDutta9 ай бұрын
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...
@DevanshiKapla Жыл бұрын
instead of the check func can we use inbuilt power func that will also work
@takeUforward Жыл бұрын
Overflow case will be an issue
@DevanshiKapla Жыл бұрын
@@takeUforward Okay bhaiya
@YourCodeVerse Жыл бұрын
Understood✅🔥🔥
@esmamanyt7048 Жыл бұрын
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
@srilathareddy94509 ай бұрын
wow explanation
@harshilkaria74203 ай бұрын
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 ?
@lakshmiprasanna7058 Жыл бұрын
Understood 💯💯💯
@VivekSharma-sk3vp9 ай бұрын
understood!! nicely
@puzzledbird9650Ай бұрын
another approach with same concept int nthRoot(int n, int m) { int low = 1; int high = m; int ans = 1; while (high >= low) { int mid = (high + low) / 2; if(pow(mid, n) == m) return mid; if (pow(mid, n) > m) { high = mid - 1; } else { low = mid + 1; } } return -1; }
@puzzledbird9650Ай бұрын
successfully passed all the test cases...
@damnrish4873Ай бұрын
@@puzzledbird9650 not sure how? this should have been overflowed "if (pow(mid, n) > m) ".
@akshatsamdani Жыл бұрын
Isnt the time complexity for this code is O(nlogm)
@SYCOA12CHAITANYAASOLE7 ай бұрын
Understood !! 😎😎
@CodewithKing3605 ай бұрын
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😄
@afrazurrahmantial68055 ай бұрын
Not some, only 1 iteration
@THIRUORUVAMMEYАй бұрын
int ans = 0; for(int i=1;i
@NazeerBashaShaik9 ай бұрын
Understood, thank you.
@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
@senseiAree Жыл бұрын
Understood ❤
@viveksoni682310 ай бұрын
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-ry8tk10 ай бұрын
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
@viveksoni682310 ай бұрын
@@HimanshuYadav-ry8tk thanks a lot
@sanjanaadepu60122 күн бұрын
Understoooood!
@theundisput3d11 күн бұрын
thank u so much striver
@sahadevnai80405 ай бұрын
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; } }
@harshilpatel32054 ай бұрын
Understood 🙏🏻
@mangeshtale28183 ай бұрын
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??
@Durt_kiloz.9643Ай бұрын
Why cannot we take high as m / 2 as it takes less no. Of checks
@sushyyyyyyyy7 күн бұрын
take example n = 1, m = 10, if u do m/2, then you won't be able to get 10 which is answer...so here its not possible
@harsh_parmar60110 ай бұрын
Understood 🎉
@shashankadiga7796 Жыл бұрын
does anyone know where to use low
@oyeshxrme5 ай бұрын
understood bhaiya
@music-loverFam5 ай бұрын
Understood😊
@modiji8706 Жыл бұрын
able to write this code myself
@diponchetia93818 ай бұрын
understood clearly
@KeepCoding693 ай бұрын
It's better to keep high as m/n. We can reduce the number of checks using this.
@RaunitJaiswal-s9v3 ай бұрын
Last for today 😮
@gopimalisetti7900 Жыл бұрын
Understood! 😀
@chiragbansod825210 ай бұрын
UNDERSTOOD
@sharubs9907 Жыл бұрын
What is square root of -6
@Learnprogramming-q7f11 ай бұрын
Thank you Bhaiya
@shaiksoofi374110 ай бұрын
understood Thank you
@AbhishekBhattacharjee-j2m Жыл бұрын
UNDERSTOOD
@priyaanshusoni8 ай бұрын
why're we returning 2 ?
@dreamyme543 Жыл бұрын
understood🙃
@thatsathishkumar Жыл бұрын
Nice bhai
@dipanshuraj78688 ай бұрын
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; }
@navinvenkat34046 ай бұрын
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 ?
@ArpanChakraborty-do6yz11 ай бұрын
just op❤🔥💯
@dayashankarlakhotia4943 Жыл бұрын
Understood please solve some hard problems
@fenilpatel62387 ай бұрын
#include int NthRoot(int n, int m) { // Write your code here. int low = 1, high = m; while(low
@akshatgosain3831 Жыл бұрын
i used pow function and overflow didnt happen,why??
@vaibhavpandey9779 Жыл бұрын
Because of weak testcases
@DeadPoolx17124 ай бұрын
UNDERSTOOD;
@meenakshitewary70152 ай бұрын
God explantion
@padmavatishah992210 ай бұрын
Understood sir
@zaffarzeshan130822 күн бұрын
public class Solution { public static int NthRoot(int n, int m) { // Write your code here. int l=1; int h=m; while(l
@srijall Жыл бұрын
Why 69 as an example 😅?
@VsEdits59 Жыл бұрын
💀
@AbhishekKumar-nz9dn Жыл бұрын
fantasy no.
@codeman38284 ай бұрын
Understood.
@nowornever85524 ай бұрын
Understoood
@ayushirastogi9725 Жыл бұрын
yep understood!!
@shubhamkumar170 Жыл бұрын
Understood!
@Manishgupta200 Жыл бұрын
Dealing with the overflow case is too tricky. That's kind of thing is taughts you only