Binary Exponentiation | Pow(x,n) | Leetcode #50
ฝัง
- เผยแพร่เมื่อ 14 ต.ค. 2024
- This video explains the most optimal technique to find pow(x,n) using the binary exponentiation technique. We have shown you the bruteforce followed by the optimal technique. We will learn binary exponentiation from scratch. This is one of the most important math algorithm and a very frequently asked interview problem.
CODE LINK is present below as usual. If you find any difficulty or have any queries then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
======================================PLEASE DONATE=============================
🧡 SUPPORT OUR WORK: / techdose
💚 UPI-ID: surya.kahar@ybl
💞JOIN Membership: / @techdose4u
==============================================================================
INSTAGRAM : / surya.pratap.k
LinkedIn: / surya-pratap-kahar-47b...
WEBSITE: techdose.co.in/
TELEGRAM Channel LINK: t.me/codewithT...
TELEGRAM Group LINK: t.me/joinchat/...
=======================================================================
USEFUL LINKS:
🟠Must do TIPS to ACE Virtual Interview: • 🔴Must do Tips to ACE y...
🟢Best strategy to excel your coding interview: • 🔴Best strategy to exce...
🟡Get your dream job in 1 month: • 🔴Get your dream job in...
🔵How to crack dream job in just 2 months: • How to crack dream job...
🟣7 Days DSA plan: techdose.co.in...
RELATED LINKS:
CODE LINK: gist.github.co...
'Squaring the base and taking half of the exponential' - You summed up the algorithm very well :)
Nice explanation. However there's one edge case added on this LC problem.
If given number is negative then take the absolute value but store this as long to avoid overflow.
At the end return accordingly.
amazing! I finally understood the iterative approach
can you please tell me which software are you using in this video to teach .
Thank you soo much sir. Amazing Explanation🙌
Great Explanation!
great explanation;
how can double hold big value in "ans" variable? it will overflow
Nice thanks for sharing
nice explanation
Thanks and welcome
Great!
Does anyone took techdose DSA course 0:31?
How much it costs?
What is timings of live classes?
Bro techdose ke playlist me videos hai, usme dekho sab bataya hai
😮
where is the code of even exponent
Please do dry run on the given code for both odd & even.
'''class Solution {
public double myPow(double x, int n) {
//exponentiation
double result=1;
int m=Math.abs(n);
while(m>=1){
if(m%2==1){
result=result*x;
}
x=x*x;
m=m/2;
}
return (n
Use long data type..
after result=result*x; write m=m-1;
// here is the correct one
class Solution {
public double myPow(double x, int n) {
double result = 1.0;
long m = Math.abs((long) n); // Convert n to long to handle Integer.MIN_VALUE
while (m > 0) {
if (m % 2 == 1) {
result *= x;
}
x *= x;
m /= 2;
}
return (n < 0) ? 1 / result : result;
}
}
//The issue is with the line m = m / 2;. In the exponentiation by squaring algorithm, you typically divide the exponent n by 2 in each iteration. However, in your code, you are modifying m instead of n. As a result, the algorithm won't work correctly for negative exponents.