POW(x,n) | Binary Exponentiation | Leetcode

  Рет қаралды 225,742

take U forward

take U forward

Күн бұрын

Пікірлер: 209
@takeUforward
@takeUforward 4 жыл бұрын
Understood or not ? Drop a Yes or No :D .. . Check me out at Instagram: instagram.com/striver_79/ . . If you appreciate the channel's work, you can join the family: bit.ly/joinFamily
@abhiprajapati2835
@abhiprajapati2835 4 жыл бұрын
Make videos faster
@sahilchoudhary1473
@sahilchoudhary1473 4 жыл бұрын
understood
@mutyamreddybejjanki8307
@mutyamreddybejjanki8307 4 жыл бұрын
Great job bro 🙌
@umapandey6796
@umapandey6796 4 жыл бұрын
yes
@mohammedwaseem8599
@mohammedwaseem8599 2 жыл бұрын
Yes.
@rishitiwari6200
@rishitiwari6200 4 жыл бұрын
Bro don't end this series and please ignore dislikers and don't get demotivated as I came to know about many technique to solve same problem which I submitted in brute force approach
@abhishekkmandal
@abhishekkmandal 4 жыл бұрын
sound quality has exponentially improved :)
@iampatelajeet
@iampatelajeet 4 жыл бұрын
Raj bhai your placement series is really really useful for all students thanks for your efforts .
@sudeshpawar733
@sudeshpawar733 4 жыл бұрын
I knew this approach but it's always better to understand it from the legend. thanks for making our life easy bro ❤️
@takeUforward
@takeUforward 4 жыл бұрын
long wla pta tha ?
@sudeshpawar733
@sudeshpawar733 4 жыл бұрын
@takeuforward Nahi Bro.. as you said this video has something for everyone.
@harshitsaxena7205
@harshitsaxena7205 Жыл бұрын
@@takeUforward that's why striver is "THE STRIVER"
@himanshumishra8740
@himanshumishra8740 3 жыл бұрын
The overflow problem can be solved without using long and just writing n=abs(n). I did this and it worked fine for me.
@rohandeshmukh3989
@rohandeshmukh3989 2 жыл бұрын
but i tried to do the way striver did that multiplying it by -1 but it was giving me the error that it cannot store that much value in int and then instead of using that i used abs and it worked but i didn't get why does it works it is same as the first approach i used but still i got wa on first approach
@visheshkaran7083
@visheshkaran7083 2 жыл бұрын
@@rohandeshmukh3989 bro typecast the right hand side with (long) it won't give you error
@satyamsrivastava.
@satyamsrivastava. 4 жыл бұрын
Nice... It is called as Fast Exponential Algorithm I had studied it during B.Tech
@Rajesh-op8zx
@Rajesh-op8zx 2 жыл бұрын
you're from which college coz even our IITs dont tech DS ,Algo to us
@aakarsrivastava7193
@aakarsrivastava7193 2 жыл бұрын
Yeah man, I'm in IIT doing final year of B.Tech+M.Tech in Computer Science and still I've to come to this video to teach myself.
@satyamsrivastava.
@satyamsrivastava. 2 жыл бұрын
So I was preparing for Gate during B.tech at that time, I studied this in Algorithms subject by myself. Same my college didn't teach me this.
@vibhor3049
@vibhor3049 3 жыл бұрын
Using recursion- JAVA static double pow(double x, long n){ if(n == 0) return 1; if(n%2 == 0) return pow(x*x, n/2); else return x*pow(x, n-1); } When the function returns the result, do check if n was +ve or -ve and show output accordingly.
@bisatisrilatha7957
@bisatisrilatha7957 3 жыл бұрын
why use double here and why long for n?????????
@democratcobra
@democratcobra 3 жыл бұрын
Negative Handel hota hein keya/?
@rajrangwani5669
@rajrangwani5669 4 жыл бұрын
Very nice video...already knew the logn solution but the overflow condition was new which could've easily ruined the interview.......please continue on this series it helps a lot
@nilesh69420
@nilesh69420 2 жыл бұрын
Congrats Sir. Big fan.
@2018-y8t
@2018-y8t 4 жыл бұрын
Keep going! This is really helpful!
@aaryannigam9552
@aaryannigam9552 Жыл бұрын
I think there is some fault in c++ solution, the if condition is wrong
@studyacc1234
@studyacc1234 2 ай бұрын
you didn't explain the recursive one..
@AnkitRaj-ux7lv
@AnkitRaj-ux7lv Ай бұрын
//correct code best excpected by any company class Solution { public: double myPow(double x, long long n) { double ankit= pow(x,n)1.0; if(n
@paritoshlambat656
@paritoshlambat656 2 жыл бұрын
Do u have any idea bitwise operator use that
@divaynagar1929
@divaynagar1929 4 жыл бұрын
Can I also do the bitwise operators approach , that also works in O(logn) as the number of bits in a number is O(logn) , Please Tell can I do it for that also ??
@ravishankar9247
@ravishankar9247 2 жыл бұрын
Why have you kept this question in the ARRAYS section of the SDE sheet ??
@kaustavpaul9306
@kaustavpaul9306 2 жыл бұрын
Brute Force class Solution { public double myPow(double x, int n) { double ans = x; if(n > 0){ for(int i=1; in; i--) ans = ans / x; } return ans; } }
@syagni_
@syagni_ 2 жыл бұрын
dont know why, but even after copy-pasting your code i am getting "time limit exceeded" in submission :/
@kaustavpaul9306
@kaustavpaul9306 2 жыл бұрын
@@syagni_ yaa thats y its Brute force
@amitagrawal4660
@amitagrawal4660 4 жыл бұрын
All cases got passed except the one below: x = 2.00000 n = -2147483648 Output = 1.00000 Expected = 0.00000
@deepakchoudhary6593
@deepakchoudhary6593 4 жыл бұрын
use long num= n; because Integer.MAX_VALUE is 2147483647 and Integer.MIN_VALUE is -2147483648 and when you convert to positive it overflows for Int. Hope you understood.
@AnkitSingh-ut7et
@AnkitSingh-ut7et 3 жыл бұрын
@@deepakchoudhary6593 but the idea is not to use trick and compute answer but to find some generic solution to handle such edge case.
@yatinarora1252
@yatinarora1252 3 жыл бұрын
Hey u can use it in a edge case if n>INT_MIN return 0.0000
@mutyamreddybejjanki8307
@mutyamreddybejjanki8307 4 жыл бұрын
Don't dislike , he is doing a great job ❤ thank you bhai 😎🙌
@AnkitKumar-ix3fl
@AnkitKumar-ix3fl 4 жыл бұрын
This is what we do in binary exponentiation. Bro your videos is very helpful. Keep making this type of content and if u can please make series on dp.
@gamingwithhemend9890
@gamingwithhemend9890 2 жыл бұрын
Where is this series in your channel. Please share the playlist link (placement series) here
@deep90402
@deep90402 2 жыл бұрын
Why we r making power +ve when it is negative??
@priyankavasam8769
@priyankavasam8769 Жыл бұрын
this logic is just mind blowing.. I wasn't able to think in that angle
@vishwa5829
@vishwa5829 11 күн бұрын
better explain using recursion tree.easy to understand
@AdityaSharma-er3gs
@AdityaSharma-er3gs 2 ай бұрын
Can't we just use in build pow() ?
@aryamanjaiswal5163
@aryamanjaiswal5163 3 жыл бұрын
Hello bhaiya, can i know y r u doing this=> ans = (double)(1.0)/(double)(ans), ans and 1.0 are already double then y type casting required?
@shivampurbia6169
@shivampurbia6169 3 жыл бұрын
Why this method and recursion is taking 20ms but inbuilt pow() or ** operator is taking 12 ms why the later one is fast ?
@arnabbiswas5472
@arnabbiswas5472 Жыл бұрын
I think the solution will not work for n=INT_MIN.
@alokmishra8786
@alokmishra8786 3 жыл бұрын
thank you for the best explanation and this is very helpful
@MsKoniki
@MsKoniki 4 жыл бұрын
please keep posting bro . really need your help to get placed. still i am begginer into competeive coding
@aysams2
@aysams2 Жыл бұрын
Hey, did you get placed? What company do you work in now?
@MsKoniki
@MsKoniki Жыл бұрын
@@aysams2 Thankfully yes , i got placed in serviced based MNC and switched to a fintech after that .
@adarshrai9516
@adarshrai9516 4 жыл бұрын
Thanks for everything ❤️❤️❤️❤️
@kanishkanim
@kanishkanim 4 жыл бұрын
Appreciable....your videos are helping a lot....carry on!!! 🤩🤩
@ranasauravsingh
@ranasauravsingh 2 жыл бұрын
UNDERSTOOD... !!! Thanks striver for the video... :)
@rohitrai2133
@rohitrai2133 3 жыл бұрын
why there are so many dislikes in leetcode for this problem!!
@alexmercerind
@alexmercerind 2 жыл бұрын
Thanks! I was using a recursive approach before. I wonder how to handle mod now. EDIT: Nevermind. Thanks for great content.
@govindsharma7696
@govindsharma7696 3 жыл бұрын
Bhaiya aapse ek request h aap apni 1st year se 4th year / placement tak ki journey share kijiye .. Aap bhi batana ki aapne kya kya problem face ki starting m aapne kese kese overcome kiya ... Kya kya resources use kiye .. and so on .. I know ki sabki journey alag h and har koi ek hi jesi journey nhi follow krta but hume thoda idea lagega ki hume kitni mehnat ki jarurat h tier 3 se achi company tak jane k liye ... Pls do this na bhaiya it's a humble request to u
@anveshasharma2726
@anveshasharma2726 2 жыл бұрын
Shall we do this with recursion in interview?
@lelouchlemprouge6380
@lelouchlemprouge6380 7 ай бұрын
Math.exp(n*Math.log(x)); Ye karne se ajaega aur x positive negative ka sign bit save krdena i think this is fastest?
@AkashSingh-mk7od
@AkashSingh-mk7od 4 жыл бұрын
Very nicely explained.... it's clearly understood....thankyou bhaiya....your series can definitely help someone feel confident and work more hard.
@roshni9320
@roshni9320 2 жыл бұрын
This solution is still giving me TLE for the long long test case!
@ShreyaSingh-vr9qi
@ShreyaSingh-vr9qi 4 жыл бұрын
Nice explaination !! bs bhaai videos rokna nahi puri 180 days complete karna.. Thanks so much for helping..
@blitzkrieg5454
@blitzkrieg5454 3 жыл бұрын
Hats off to all your hardwork, keep going!
@abhinavmishra7617
@abhinavmishra7617 4 жыл бұрын
Although the complexity of both approaches is identical, this approach will be faster in practice since we have the overhead of the recursive calls.
@DR-mq1le
@DR-mq1le Жыл бұрын
in the else condition should it not be "ans=x*x" ?
@amansinghal4663
@amansinghal4663 Жыл бұрын
My recursive solution ( accepted in leetcode ) class Solution { public: double func(double x, int n){ if(n==0){ return 1; } if(n%2 == 0){ return func(x*x, n/2); } else{ return x*func(x*x, n/2); } } double myPow(double x, int n) { if(n
@neelesh621
@neelesh621 8 ай бұрын
the fun faCT is, there is no difference between c++ & java solution
@davegupta329
@davegupta329 2 ай бұрын
double myPow(double x, int n) { double pow; pow = exp(n * log(fabs(x))); if (x < 0 && n % 2 != 0) pow = -1 * pow; return pow; } Can it be considered?
@mohammedwaseem8599
@mohammedwaseem8599 2 жыл бұрын
This code is giving Wrong Answer.
@takeUforward
@takeUforward 2 жыл бұрын
The code works fine..
@deepakavva317
@deepakavva317 4 жыл бұрын
I think a series on dynamic programming can increase your subscribers.
@ramsai6141
@ramsai6141 4 жыл бұрын
yes brother...so many people are waiting for your DP series
@devangpapinwar208
@devangpapinwar208 3 жыл бұрын
Thank you Trevor
@Morimove
@Morimove Жыл бұрын
this video is linked to recursion on DSA A2Z series but the shown solution is iterative
@takeUforward
@takeUforward Жыл бұрын
Yes because this can also be solved using recursion. I will soon add
@Morimove
@Morimove Жыл бұрын
@@takeUforward thankyou sir
@amd7407
@amd7407 4 жыл бұрын
bro awesome videos ,your sheet helps me a lot , little sound can be improved . But please keep making videos .
@raholsaha8191
@raholsaha8191 4 жыл бұрын
Nice work bro. You are doing a great job.
@pleasesirmorevideos4684
@pleasesirmorevideos4684 4 жыл бұрын
Why dislikes???
@DR-mq1le
@DR-mq1le Жыл бұрын
in the article the c++ code is wrong ,
@chandlerbing8164
@chandlerbing8164 Жыл бұрын
not accepted by leetcode submission please explain floating point power as well brother
@himanshidixit6406
@himanshidixit6406 2 жыл бұрын
Completely understood..Thank you so much..:)
@samqureshi9415
@samqureshi9415 4 жыл бұрын
First time dekha video isse phoe ds or algo kya hai pata he nahi tha . Per dekh kar acha laga ❤️
@mdmuquimakhter5145
@mdmuquimakhter5145 2 жыл бұрын
class Solution { public: double myPow(double x, int n) { long long num=n; double ans=1.0; if(num
@wisdomkhan
@wisdomkhan 2 жыл бұрын
how is this interview specific?
@omanshsharma6796
@omanshsharma6796 2 ай бұрын
The quality of explanations seems to be inversely related to the quality of mic :D
@swetantkumar7029
@swetantkumar7029 4 жыл бұрын
Following ur series and waiting next video to come eagerly....Great job man.
@sanskarghyar9408
@sanskarghyar9408 Жыл бұрын
class Solution { public: double myPow(double x, int n) { if(n
@Techna-to5qt
@Techna-to5qt Жыл бұрын
what if n is a real number?
@SHIVAMGUPTA-wb5mw
@SHIVAMGUPTA-wb5mw 4 жыл бұрын
Following ur series ...plz continue
@kaichang8186
@kaichang8186 10 күн бұрын
understood, thanks for the awesome video
@WasimAkram-hb5lr
@WasimAkram-hb5lr 4 жыл бұрын
Brother your is really helpful keep going and ignore hate comments
@akashnarvaria3663
@akashnarvaria3663 2 жыл бұрын
Why we have to take long nn? With int nn it is not working Can someone explain?
@shreyash8273
@shreyash8273 2 жыл бұрын
Int will overflow for the -intMin value when will make it positive
@deepapandey849
@deepapandey849 2 жыл бұрын
Amazing series + solutions, hats off! Just, this video has very annoying sound quality :(
@chakshusalgotra
@chakshusalgotra 4 жыл бұрын
thanks all to u sir u been a great help
@parikshitrathore1510
@parikshitrathore1510 4 жыл бұрын
In some places the voice of previous and succeding transitions are overlapping!!
@parikshitrathore1510
@parikshitrathore1510 4 жыл бұрын
But, understood!!
@takeUforward
@takeUforward 4 жыл бұрын
@@parikshitrathore1510 Yeah am using a new editor hence this is happening, will check this bro in the next one !
@dhruvrajput5194
@dhruvrajput5194 4 жыл бұрын
Really great explanation.. But i think there is some problem with your mic.. Or maybe something's wrong with my earphones😕
@takeUforward
@takeUforward 4 жыл бұрын
Some problem on my side!
@ketanahuja8939
@ketanahuja8939 4 жыл бұрын
bhiaya not got this line x = x* x; and we are doing nn/=2;
@linhinNTU
@linhinNTU Жыл бұрын
can anyone explain for me why n needs to be " long long" type?
@jiviteshvarshney3644
@jiviteshvarshney3644 Жыл бұрын
For saving large int
@sheikhaman6218
@sheikhaman6218 4 жыл бұрын
Always a helping hand... 😇
@Rahul-tk8ck
@Rahul-tk8ck 4 жыл бұрын
U r helping me alot bro.stay safe✨
@sumitkulyal4102
@sumitkulyal4102 4 жыл бұрын
Good work bhaiyya. Haters are doing great job, bhaiyya it's means your are doing great job more haters means you are doing right work. Keep going bhaiyya.
@samarthajadhao1176
@samarthajadhao1176 4 жыл бұрын
can make videos on C++ tricks...
@sarthak1317
@sarthak1317 3 жыл бұрын
The voiceover at 2:22 xD
@letsexplore7590
@letsexplore7590 3 жыл бұрын
yes
@yashkhatwani3198
@yashkhatwani3198 2 жыл бұрын
Thank you
@lakshkaushik6366
@lakshkaushik6366 4 жыл бұрын
Can u help me by guiding me how to start doing this stuff I am in 12 th now
@39_jatinjain4
@39_jatinjain4 Жыл бұрын
can anyone plz tell me where i am doing wrong?? class Solution { public: double sol(double x,long n){ // base case if(n==0) return 1; return x*sol(x,n-1); } double myPow(double x, int n) { long z=abs(n); if(n%2!=0) z=z+1; double t= sol(x,z/2); double ans=t*t; if(n%2!=0) ans=ans/x; if(n
@RaaheeAmit
@RaaheeAmit 4 жыл бұрын
U help us alot
@divyanshigupta1568
@divyanshigupta1568 2 жыл бұрын
You are truly a legend 👏🏻
@nopecharon
@nopecharon 2 жыл бұрын
Thanks
@AshishKumar-me8pr
@AshishKumar-me8pr 4 жыл бұрын
Stiver_79 you are Genius.. excellent
@redhathacker3458
@redhathacker3458 3 жыл бұрын
Yes
@atulmalakar
@atulmalakar 4 жыл бұрын
Naming of variables could have been better.
@BRSanush
@BRSanush 3 жыл бұрын
Happy teachers day Striver sir🎉
@het314
@het314 2 жыл бұрын
using long is kinda cheating. Try diving when n
@israkulhaquetusher3256
@israkulhaquetusher3256 4 жыл бұрын
SDE sheet. link isn't working
@tannukumari5144
@tannukumari5144 4 жыл бұрын
great work bhaiya, thank you
@abhinavmishra7617
@abhinavmishra7617 4 жыл бұрын
why didn't we do binary exponentiation? fast_power.... pow(a,n){ base cases b=pow(a,n/2); if(n%2==1) return b*b*a; return b*b; }
@takeUforward
@takeUforward 4 жыл бұрын
Watch the video, you wrote the recursive solution for the same. Dont learn the codes, understand the algo, then you will know that both are same!
@abhinavmishra7617
@abhinavmishra7617 4 жыл бұрын
@@takeUforward bhai maine yaad to nahi kiya h....ye kaise bol diye aap? n if mai apne doubt clear krne k liye comment krunga to tum aise reply karoge? not cool
@takeUforward
@takeUforward 4 жыл бұрын
Nai bro, aapne pucha binary exponentiation kyu use nai kia aapne. Yahi comment tha? Th mne suggestion dia. Kyu ki agar algos smjhoge th ye puchte nai. Baaki tum isse constructive loge toh next time sayad tm analysis kroge :) rest me chahta th comment ka ni v reply kr skta tha.. but me kia so that tm smjho ye chiz ki tm algos smjhe ni. Logic tmhre code ki smjhte th ye question aati hi nai as dono same. Bs video wali iterative h tumhri code recursive!!
@abhinavmishra7617
@abhinavmishra7617 4 жыл бұрын
@@takeUforward ok bhai... actually mere liye recursive zyada intuitive h maybe isliye main iteratively nhi smjh paya isliye bs maine puchha
@takeUforward
@takeUforward 4 жыл бұрын
Abhinav Mishra intuitive ho sakta hai but agar tumhe pta h n odd p n-1 kr rhe and n even pe n/2, toh ye recursion me kyu kr rhe ho? Wahi reason mene bataya h. Th dono same hua na ?? Interviews m agar tum ase dono ko alag bologe th dikkat ho skti, iskie ache se analyse kro, like try to write both iterative and recursive
@Learnprogramming-q7f
@Learnprogramming-q7f 7 ай бұрын
Thank you Bhaiya
@enigma2886
@enigma2886 3 жыл бұрын
thanks !
@MaheshPatil-of1zy
@MaheshPatil-of1zy 7 ай бұрын
its 2024 feb i am watching 😊
@aditya_sipani
@aditya_sipani 5 ай бұрын
what is nn here,like ?
@Ankitkumar-gc5cc
@Ankitkumar-gc5cc 3 ай бұрын
That is just storing value of n so that while we change the value of nn in loop we can retain value of n and that can be used for final return statement where he is checking if it's negative or positive
@tejasjoshi1907
@tejasjoshi1907 3 жыл бұрын
Why this question has so many dislikes on leetcode.??
@gauravidesigns
@gauravidesigns 4 жыл бұрын
Thankyou bro