BS-11. Find the Nth root of an Integer

  Рет қаралды 130,534

take U forward

take U forward

Күн бұрын

Пікірлер: 232
@andrewbeef8758
@andrewbeef8758 Жыл бұрын
this is a hidden gem playlist in entire youtube
@nehathakur40
@nehathakur40 Жыл бұрын
The most simplified explanation one could ever get!
@HarshKumar-ip5nr
@HarshKumar-ip5nr Жыл бұрын
The edge case of overflow was amazing. Thank you for such amazing content. Consider completing the series we are eagerly waiting.
@mrsttechno5974
@mrsttechno5974 Жыл бұрын
Your teaching style is awesome, I understand everything in detail
@joeljacob4685
@joeljacob4685 Жыл бұрын
Handling the overflow case was amazing !! I didn't got that one
@Bigg_boss_trolls
@Bigg_boss_trolls 9 күн бұрын
our teaching style is awesome
@bharatmehta30
@bharatmehta30 Жыл бұрын
Was missing the overflow case. Thanks to this amazing video.
@harshmagarwal3196
@harshmagarwal3196 6 ай бұрын
Sometimes I feel he is scolding me. But honestly this is one of the best playlist available on YT.
@VarshaSingh-hi2sb
@VarshaSingh-hi2sb 3 ай бұрын
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 2 ай бұрын
@@VarshaSingh-hi2sb it feels lyk u are scolding 😅
@KrishnaKumar-b4m9p
@KrishnaKumar-b4m9p 3 күн бұрын
@@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)
@dank7044
@dank7044 Жыл бұрын
suppose M=10^63-1;( max value of long long), and mid=10^63/2, now is mid
@girikgarg8
@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; }
@cinime
@cinime Жыл бұрын
Understood! Super amazing explanation as always, thank you so so much for your effort!!
@suryanshsoni3420
@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.
@yashaggarwal6013
@yashaggarwal6013 9 ай бұрын
what is the logic behind m / (Math.pow(mid, n - 1) ?
@suryanshsoni3420
@suryanshsoni3420 9 ай бұрын
@@yashaggarwal6013 mid * mid**(n-1) = mid**n
@omkarsawant9267
@omkarsawant9267 9 ай бұрын
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.
@AtulKumar-c4x7l
@AtulKumar-c4x7l Жыл бұрын
understood Thank you striver for such an amazing explanation.
@KeepCoding69
@KeepCoding69 2 ай бұрын
It's better to keep high as m/n. We can reduce the number of checks using this.
@AayushRana-kl4lj
@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.
@manavsingh5919
@manavsingh5919 Жыл бұрын
Thank you Striver....Understood everything🙂
@hareshnayak7302
@hareshnayak7302 8 ай бұрын
Understood,Thanks striver for this amazing video.
@johndurai2226
@johndurai2226 Жыл бұрын
I am your big fan because of your videos are well and explanation also
@NitinKumar-wm2dg
@NitinKumar-wm2dg Жыл бұрын
understood bhaiya, thank you for this tutorial
@venkateshcharyakaram6547
@venkateshcharyakaram6547 2 ай бұрын
explaining the concepts excellent
@AnmolGupta-oj4lm
@AnmolGupta-oj4lm Жыл бұрын
Understood Very Well!
@priyanshuchoudhary246
@priyanshuchoudhary246 Жыл бұрын
Amazing Playlists
@kunalsingh8796
@kunalsingh8796 Жыл бұрын
consistency to gave such awesome videos makes u a good youtuber ...keep doing sir
@DivyamGupta-ly4ds
@DivyamGupta-ly4ds Жыл бұрын
oh i really see that coming....😂😂
@purushottam108
@purushottam108 5 ай бұрын
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
@KrishnaKumar-b4m9p 3 күн бұрын
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
@shoaibaltaf1128
@shoaibaltaf1128 2 ай бұрын
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
@srilathareddy9450
@srilathareddy9450 7 ай бұрын
wow explanation
@karthik-varma-1579
@karthik-varma-1579 3 ай бұрын
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; } }
@HardikDoshi-ml8ii
@HardikDoshi-ml8ii 5 ай бұрын
can we use power func --- pow(mid,n) ??? instead of writing different function??
@THIRUORUVAMMEY
@THIRUORUVAMMEY 5 күн бұрын
int ans = 0; for(int i=1;i
@captainakash8914
@captainakash8914 6 ай бұрын
thanks you striver for.... easy explanation
@oyeesharme
@oyeesharme 3 ай бұрын
understood bhaiya
@graviton001
@graviton001 4 ай бұрын
Solved this too myself thnx ❤
@ashutoshranjan4644
@ashutoshranjan4644 Жыл бұрын
In python, it doesn't cause any issue with that code. But thanks for that edge case of c++.
@KarthikNandam-xs4qn
@KarthikNandam-xs4qn Ай бұрын
understood my g 🔥
@harshilpatel3205
@harshilpatel3205 3 ай бұрын
Understood 🙏🏻
@codeman3828
@codeman3828 3 ай бұрын
Understood.
@justcodeitbro1312
@justcodeitbro1312 Жыл бұрын
Damn bhai what a great explanation thanks alot
@TrinmoyDutta
@TrinmoyDutta 8 ай бұрын
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...
@DeadPoolx1712
@DeadPoolx1712 3 ай бұрын
UNDERSTOOD;
@music-loverFam
@music-loverFam 3 ай бұрын
Understood😊
@VivekSharma-sk3vp
@VivekSharma-sk3vp 7 ай бұрын
understood!! nicely
@diponchetia9381
@diponchetia9381 6 ай бұрын
understood clearly
@nowornever8552
@nowornever8552 2 ай бұрын
Understoood
@NazeerBashaShaik
@NazeerBashaShaik 7 ай бұрын
Understood, thank you.
@Learnprogramming-q7f
@Learnprogramming-q7f 9 ай бұрын
Thank you Bhaiya
@chiragbansod8252
@chiragbansod8252 8 ай бұрын
UNDERSTOOD
@YourCodeVerse
@YourCodeVerse 11 ай бұрын
Understood✅🔥🔥
@senseiAree
@senseiAree Жыл бұрын
Understood ❤
@sarangkumarsingh7901
@sarangkumarsingh7901 6 ай бұрын
Understood...............
@savitore
@savitore 7 ай бұрын
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
@mcbotface Жыл бұрын
Can you share the code where you modify exponentiation function to handle overflow situation instead of naive multiplication function?
@SYCOA12CHAITANYAASOLE
@SYCOA12CHAITANYAASOLE 5 ай бұрын
Understood !! 😎😎
@harsh_parmar601
@harsh_parmar601 9 ай бұрын
Understood 🎉
@lakshmiprasanna7058
@lakshmiprasanna7058 Жыл бұрын
Understood 💯💯💯
@meenakshitewary7015
@meenakshitewary7015 Ай бұрын
God explantion
@yashaggarwal6013
@yashaggarwal6013 9 ай бұрын
Why can't we use pow(mid,n) to calculate power instead of writing an entire function in C++?
@RAHULMAHESHWARIdexter
@RAHULMAHESHWARIdexter 6 ай бұрын
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)
@modiji8706
@modiji8706 Жыл бұрын
able to write this code myself
@shaiksoofi3741
@shaiksoofi3741 8 ай бұрын
understood Thank you
@padmavatishah9922
@padmavatishah9922 8 ай бұрын
Understood sir
@dreamyme543
@dreamyme543 Жыл бұрын
understood🙃
@shubhamkumar170
@shubhamkumar170 Жыл бұрын
Understood!
@rushithagudipudi7742
@rushithagudipudi7742 7 ай бұрын
Understand
@AbhishekBhattacharjee-j2m
@AbhishekBhattacharjee-j2m Жыл бұрын
UNDERSTOOD
@thatsathishkumar
@thatsathishkumar Жыл бұрын
Nice bhai
@CodewithKing360
@CodewithKing360 3 ай бұрын
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😄
@afrazurrahmantial6805
@afrazurrahmantial6805 3 ай бұрын
Not some, only 1 iteration
@AmanKumar-qz4jz
@AmanKumar-qz4jz Жыл бұрын
thanks
@ayushirastogi9725
@ayushirastogi9725 Жыл бұрын
yep understood!!
@drizzle01
@drizzle01 Жыл бұрын
understood
@ShubhanshuG007
@ShubhanshuG007 Жыл бұрын
Understood
@AtulKumar-c4x7l
@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
@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
@MohitKumar-o3l1u
@MohitKumar-o3l1u 6 ай бұрын
Understood!!
@humanity7880
@humanity7880 Жыл бұрын
understood!
@rupalikumari8829
@rupalikumari8829 4 ай бұрын
Understood:)
@amitranjeetjha1240
@amitranjeetjha1240 Жыл бұрын
Understood
@shaswatshah4920
@shaswatshah4920 5 ай бұрын
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
@gopimalisetti7900
@gopimalisetti7900 Жыл бұрын
Understood! 😀
@utsavseth6573
@utsavseth6573 Жыл бұрын
Lovely.
@random_akki
@random_akki Жыл бұрын
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
@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
@ArpanChakraborty-do6yz
@ArpanChakraborty-do6yz 9 ай бұрын
just op❤‍🔥💯
@raghavkansal3765
@raghavkansal3765 Жыл бұрын
"understood"
@sahadevnai8040
@sahadevnai8040 4 ай бұрын
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; } }
@shashwat_harsh
@shashwat_harsh Жыл бұрын
understood.
@RaunitJaiswal-s9v
@RaunitJaiswal-s9v 2 ай бұрын
Last for today 😮
@akashkumaryadav1827
@akashkumaryadav1827 3 ай бұрын
🔥
@DevanshiKapla
@DevanshiKapla Жыл бұрын
instead of the check func can we use inbuilt power func that will also work
@takeUforward
@takeUforward Жыл бұрын
Overflow case will be an issue
@DevanshiKapla
@DevanshiKapla Жыл бұрын
@@takeUforward Okay bhaiya
@kushagramishra5638
@kushagramishra5638 Жыл бұрын
UnderStood!
@suryasaimaheswar8636
@suryasaimaheswar8636 4 ай бұрын
understood:)
@esmamanyt7048
@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
@prthms_
@prthms_ 9 ай бұрын
i got it
@MischievousSoul
@MischievousSoul Жыл бұрын
Cfbr
@googleit2490
@googleit2490 Жыл бұрын
Understood :)
@dayashankarlakhotia4943
@dayashankarlakhotia4943 Жыл бұрын
Understood please solve some hard problems
@mangeshtale2818
@mangeshtale2818 2 ай бұрын
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??
@Manishgupta200
@Manishgupta200 Жыл бұрын
Dealing with the overflow case is too tricky. That's kind of thing is taughts you only
@akshatsamdani
@akshatsamdani Жыл бұрын
Isnt the time complexity for this code is O(nlogm)
@suyashmishra6946
@suyashmishra6946 Жыл бұрын
Understood!
@raghavkansal3765
@raghavkansal3765 Жыл бұрын
what is return 1 return 0 and return 2 i didnt get that
@yesaryann
@yesaryann Жыл бұрын
@@raghavkansal3765 same bhai
@anubhav_gupta_
@anubhav_gupta_ Жыл бұрын
US✅
@anshsaxena7297
@anshsaxena7297 3 ай бұрын
UnderStood
BS-12. Koko Eating Bananas
21:04
take U forward
Рет қаралды 187 М.
Linear & Binary Search Code | Big O Notation
19:31
Telusko
Рет қаралды 36 М.
За кого болели?😂
00:18
МЯТНАЯ ФАНТА
Рет қаралды 3,2 МЛН
BS-14. Find the Smallest Divisor Given a Threshold | Binary Search
16:00
BS-10. Finding Sqrt of a number using Binary Search
17:11
take U forward
Рет қаралды 145 М.
Please Master These 10 Python Functions…
22:17
Tech With Tim
Рет қаралды 218 М.
Programming Languages Tier List 2024
16:18
Neal Wang
Рет қаралды 11 М.
sqrt(i)
9:02
blackpenredpen
Рет қаралды 4,7 МЛН
Why is Python 150X slower than C?
10:45
Mehul - Codedamn
Рет қаралды 21 М.
BS-13. Minimum days to make M bouquets | Binary Search
26:01
take U forward
Рет қаралды 153 М.
❌ Don't Run Behind 500 LEETCODE Problems ❌ Focus on QPCD
8:31