I have been struggling with DSA from 8 months now and lost my interest in computer science but your videos helped me a lot and boost my confidence. Thanks a lot.
bhaiyya ek barr dry run krke bataado please ans=power(N,R/2) is line me power work kese karra he samajh nahi ayaa mtlb vo power calculate kese karra he ????
My thought for the return statement- if (rev ==1){ return 1; } return num * f(num, rev-1); Edit after seeing the solution: base condition me thoda mistake ho gya... But glad ki return condition sahi h...
It doesn't handle the negative power value ie Input: x = 2.00000, n = -2 Here is the complete code: (In JavaScript) and no need MOD value const myPow = (x, n) => { if (n === 0) return 1; if (n < 0) return 1 / myPow(x, -n); if (n % 2 === 0) { const halfPow = myPow(x, n / 2); return halfPow * halfPow; } else { const halfPow = myPow(x, (n - 1) / 2); return halfPow * halfPow * x; } }; // let a=2, b=10 // 1024 let a = 2.1, b = 3; //9.26100 // let a= 2.00000, b = -2 //0.25000// Explanation: 2-2 = 1/22 = 1/4 = 0.25 console.log("myPow", myPow(a, b));
bhaiya this code giving wrong ans :- class Solution{ public: //You need to complete this fucntion long long power(int N,int R) {int modu=1000000007; if(R==0)return 1; if(N==0)return 0; //Your code here if(R%2 ==0){ long long ans= power(N,R/2); return ((ans%modu)*(ans%modu))%modu; } else { long long ans= power(N,(R-1)/2); return ((ans%modu)*(ans%modu)*(N%modu))%modu; } } }; but below code running correctly after removing brackets:--- class Solution{ public: //You need to complete this fucntion long long power(int N,int R) {int modu=1000000007; if(R==0)return 1; if(N==0)return 0; if(R%2 ==0){ long long ans= power(N,R/2); return (ans%modu*ans%modu)%modu; } else { long long ans= power(N,(R-1)/2); return (ans%modu*ans%modu*N%modu)%modu; } } }; i am not able to understand the difference??please explain bhaiyaa...
// } Driver Code Ends class Solution{ public: //You need to complete this fucntion long long power(int N,int R) { int mod =1e9+7; //Your code here if(N == 0) return 0; if(R == 0) return 1; if (R %2== 0){ long long ans= power(N,(R)/2); return ((ans%mod)*(ans%mod))%mod; } else { long long ans = power(N,(R-1)/2); return ((ans%mod)*(ans%mod)*(N%mod))%mod; } } }; whats the problem with this code