Video není dostupné.
Omlouváme se.
BS-11. Find the Nth root of an Integer
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
this is a hidden gem playlist in entire youtube
The edge case of overflow was amazing. Thank you for such amazing content. Consider completing the series we are eagerly waiting.
The most simplified explanation one could ever get!
Handling the overflow case was amazing !! I didn't got that one
Sometimes I feel he is scolding me. But honestly this is one of the best playlist available on YT.
Your teaching style is awesome, I understand everything in detail
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.
Was missing the overflow case. Thanks to this amazing video.
Understood! Super amazing explanation as always, thank you so so much for your effort!!
understood
Thank you striver for such an amazing explanation.
Understood,Thanks striver for this amazing video.
suppose M=10^63-1;( max value of long long), and mid=10^63/2, now is mid
understood bhaiya, thank you for this tutorial
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;
}
I am your big fan because of your videos are well and explanation also
Thank you Striver....Understood everything🙂
Understood Very Well!
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.
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.🤩🤩🤩🤩🤩🤩❤❤😥😥
wow explanation
Amazing Playlists
Solved this too myself thnx ❤
consistency to gave such awesome videos makes u a good youtuber ...keep doing sir
oh i really see that coming....😂😂
Understood
thanks you striver for.... easy explanation
Understood😊
understood bhaiya
understood
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.
what is the logic behind m / (Math.pow(mid, n - 1) ?
@@yashaggarwal6013 mid * mid**(n-1) = mid**n
Can you share the code where you modify exponentiation function to handle overflow situation instead of naive multiplication function?
when you are modifying code for overflow, the T.C got changed to O(n*logm). How to do it in O(logn logm)?
understood🙃
can we use power func --- pow(mid,n) ??? instead of writing different function??
i thought of the same approach
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;
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
Damn bhai what a great explanation thanks alot
Understood!
Why can't we use pow(mid,n) to calculate power instead of writing an entire function in C++?
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)
understood!! nicely
understood clearly
Understood, thank you.
understood!
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
Understood !! 😎😎
understood Thank you
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😄
Not some, only 1 iteration
Understood✅🔥🔥
Understood :)
In python, it doesn't cause any issue with that code. But thanks for that edge case of c++.
instead of func can we use pow(mid,n)??
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
@@user-hq7yd4wz4y leetcode accepted the solution using pow
Understood 🎉
UNDERSTOOD
instead of the check func can we use inbuilt power func that will also work
Overflow case will be an issue
@@takeUforward Okay bhaiya
Understood ❤
Understood sir
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...
Understood 💯💯💯
Thank you Bhaiya
Understood...............
Understood❤
Understood!!
yep understood!!
able to write this code myself
understood:)
just op❤🔥💯
thanks
Understand
I have a doubt,instead of creating multiplication function,im simply using pow(mid,n) and im not having overflow
Is this fine?
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
Understood! 😀
Nice bhai
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
i got it
UnderStood!
Isnt the time complexity for this code is O(nlogm)
Lovely.
understood.
Understood please solve some hard problems
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.
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
@@HimanshuYadav-ry8tk thanks a lot
does anyone know where to use low
++thanks
#include
int NthRoot(int n, int m) {
// Write your code here.
int low = 1, high = m;
while(low
at 2:57 time complexity will be O( Nth Root (M)) right?
Yes he has corrected it in the video also
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;
}
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 ?
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;
}
}
"understood"
Cfbr
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
What is square root of -6
i used pow function and overflow didnt happen,why??
Because of weak testcases
❤
why're we returning 2 ?
Dealing with the overflow case is too tricky. That's kind of thing is taughts you only
US
US✅
Why 69 as an example 😅?
💀
fantasy no.
1st view❤❤❤❤
UNderstood
god
underStood
First viewer