The best thing about your videos is if I saw any other solution video I feel regret, but your explanation gave another level understanding and satisfaction that maine ye problem Sikh Li ab kisi ko samjha skta hu❤
Hi guys, Make sure you don't skip even a single second of this video. Especially the Story Points part 21:12 . The dots will connect and the solution will hit you. I hope we all learned something new today from this problem. ❤❤❤
I am speechless at how beautifully you explained the problem. I've watched other RUclips videos on the same problem, but I couldn't grasp the intuition behind it. They jumped straight into the problem without giving any explanation, so I struggled to write the code on my own. However, after watching your video, I finally understood the right intuition and just how simple the code is (even without watching your story points). When you said, "code toh aap likh hi loge," I was skeptical, but later I realized the significance of that statement. Thank you for making this video. You've earned yourself a new subscriber, and I will definitely recommend your video to anyone learning DSA. Hats off to you! 🫡❤
Another solution, we can use binary exponentiation to get rid of the power vector.... but each and everytime we've to call it to calculate the power(2, something) which we'll take LESS THAN SQRT(N) tc but your solution is the better one if we look at the TC.... and this one is better if we go for SPACE OPTIMISATION # CODE CPP class Solution { const int M = 1e9 + 7; int powerOf2(long long x, int n) { int power = 1; while (n) { if (n & 1) power = ( (x % M) * (power % M) ) % M; n = n >> 1; x = ( (x % M) * 1ll * (x % M) ) % M; } return power; } public: int numSubseq(vector& nums, int target) {
if we use pow(2,n)%M , then modulo doesn't help us. because, pow is calculating the value first, then it will be modulo. but when pow calculates the value, it goes past the range of integer. And when it already goes past the range of integer, the modulo has nothing to do. So we use precalculation of power. And during that precalculation, we always do modulo in each element. So the value can't go past the range of integer. another option was to use binpow. but I like this type of precalculation. This comment is not for Mazhar vai, it's for noobs like me. Thanks mazhar vai.
This extra thing of using modulo , seems very confusing to me , if you could tell more about how modulo helps in large solution , it will be very helpful
Hey bro thanks alot, spent alot of time trying to understand why it was 2^(n-1) and not (2^n)-1, dusre vids bhi dekhe bt it wasn't explained clearly at all, ab ajke samaj aya we are fixing the min value thats why, thanks bro btw can u post theae vids abit earlier please
Thank you so much ❤️❤️ And yes i will start posting earlier. Actually the delay is because of my throat infection treatment being done. I have recovered now and will be back to early videos Thank you for watching ❤️❤️
@@codestorywithMIK cool bro awesome, btw a thing i noticed as i have started spending more time in dsa is that some days my brain works great n some days it doesn't work at all and i cant solve even simple stuff, is there any fix to this or is it kind of burnout?
That’s totally totally fine. It happens. I always suggest to give oneself a break to relax and enjoy. May be Sundays , keep it free to relax and enjoy a little. And in other days, work harder like no one can ever beat you 💪💪💪
have you tried to use binaryExponentiation i feel that we can't use it as it will just increase the complexity.... and Math.pow is O(1) function and outputs the same thing that binaryExponentation will give... If you were able to make it work?? can you send me the code... because issue was that pow was too big....
@@architchanana7866 It Worked for me Here's the code: class Solution { private: const int M = 1e9+7; int binExp(int a, int b) { int ans = 1; while(b) { if(b&1) { ans = (ans * 1LL * a) % M; } a = (a * 1LL * a) % M; b >>= 1; } return ans; } public: int numSubseq(vector& nums, int target) { sort(begin(nums), end(nums)); int n = nums.size(); int s = 0, e = n-1; int ans = 0; while(s target) { e--; } else { ans = (ans + binExp(2, e-s)) % M; s++; } } return ans; } };
I used the same code without modulo and with pow() function , but i got wrong answer instead of runtime , below is the code , It is very helpful if you able to told where my code went wrong ?? int numSubseq(vector& nums, int target) { sort(nums.begin(), nums.end()); long long int out = 0; int i=0,j=nums.size()-1; while(i
Java Code: class Solution { public int numSubseq(int[] nums, int target) { Arrays.sort(nums); int l=0; int r=nums.length - 1; int mod=1000000007; int power[]=new int[nums.length]; power[0]=1; for(int i=1;i
The best thing about your videos is if I saw any other solution video I feel regret, but your explanation gave another level understanding and satisfaction that maine ye problem Sikh Li ab kisi ko samjha skta hu❤
It means a lot to me. You made my day ❤️❤️❤️
@@codestorywithMIK ♥️
Hi guys,
Make sure you don't skip even a single second of this video. Especially the Story Points part 21:12 . The dots will connect and the solution will hit you.
I hope we all learned something new today from this problem. ❤❤❤
bro applause to your courage that you honestly tell that power factor scene, really its works as a motivation for a budding coder .❤
Thank you so much shivansh ❤️❤️❤️
wow so smooth
One of the best teachers on this platform.🤩🤩
Thank you so much Archit ❤️❤️❤️
can you explain subsets, subsequences, subarrays, substrings and all in one video with an example in detail?
Sure thing. Let me plan that
@@codestorywithMIK yes 🙏
bhaiya kya apne ye video upload kari kya ?
The pre-computational part was nuts.
Same Aarav. It didn’t occur to my mind
I am speechless at how beautifully you explained the problem. I've watched other RUclips videos on the same problem, but I couldn't grasp the intuition behind it. They jumped straight into the problem without giving any explanation, so I struggled to write the code on my own. However, after watching your video, I finally understood the right intuition and just how simple the code is (even without watching your story points). When you said, "code toh aap likh hi loge," I was skeptical, but later I realized the significance of that statement. Thank you for making this video. You've earned yourself a new subscriber, and I will definitely recommend your video to anyone learning DSA. Hats off to you! 🫡❤
❤️❤️🙏🙏
can't thank you enough!
Thank you so much for watching my video 😇
You are amazing , one request is please make up solving of leetcode contests🙏🙏
Thank you so much ❤️❤️❤️
And sure contest i will try soon
ur best
THanks for the detailed solution .
Thank you for watching Alok ❤️❤️
appreciate your detailed solution!
Amazing Explanation
Thank you Anup ❤️❤️
Amazing Explanation, thank you 👍👍
Thanks a lot Sahil ❤️❤️
Another solution, we can use binary exponentiation to get rid of the power vector.... but each and everytime we've to call it to calculate the power(2, something) which we'll take LESS THAN SQRT(N) tc
but your solution is the better one if we look at the TC....
and this one is better if we go for SPACE OPTIMISATION
# CODE CPP
class Solution {
const int M = 1e9 + 7;
int powerOf2(long long x, int n) {
int power = 1;
while (n) {
if (n & 1)
power = ( (x % M) * (power % M) ) % M;
n = n >> 1;
x = ( (x % M) * 1ll * (x % M) ) % M;
}
return power;
}
public:
int numSubseq(vector& nums, int target) {
int n = nums.size();
sort(nums.begin(), nums.end());
long long int ans = 0;
int low = 0;
int high = nums.size() - 1;
while (low
best
Thank you Sir.
Thanks a lot for watching
if we use pow(2,n)%M , then modulo doesn't help us. because, pow is calculating the value first, then it will be modulo. but when pow calculates the value, it goes past the range of integer. And when it already goes past the range of integer, the modulo has nothing to do. So we use precalculation of power. And during that precalculation, we always do modulo in each element. So the value can't go past the range of integer. another option was to use binpow. but I like this type of precalculation. This comment is not for Mazhar vai, it's for noobs like me. Thanks mazhar vai.
What a summary. Thank you so so much ❤️❤️❤️
can we connect on linkedIn ?
and solution video is simply awesome.
Sure thing.
LinkedIn - www.linkedin.com/in/mazhar-imam-khan-95a34ab3
LinkedIn codestorywithMIK Page - www.linkedin.com/groups/12559380
Instagram : instagram.com/codestorywithmik/
Facebook : facebook.com/people/codestorywithmik/100090524295846/
can we use left shift operator to calculate power?
Best mann best!
Thanks a lot Aabir ❤️❤️
well explained!!!
In 2,3,3,4,6,7, when L move from value 3(L=0) to 3(L=1), don’t we get overlapping sets? Both can have 3,4,6,7. Is above statement correct?
great .
This extra thing of using modulo , seems very confusing to me , if you could tell more about how modulo helps in large solution , it will be very helpful
Sure Supriya. Let me try to make a video soon on this and explain the purpose behind modulo
Hey bro thanks alot, spent alot of time trying to understand why it was 2^(n-1) and not (2^n)-1, dusre vids bhi dekhe bt it wasn't explained clearly at all, ab ajke samaj aya we are fixing the min value thats why, thanks bro btw can u post theae vids abit earlier please
Thank you so much ❤️❤️
And yes i will start posting earlier. Actually the delay is because of my throat infection treatment being done. I have recovered now and will be back to early videos
Thank you for watching ❤️❤️
@@codestorywithMIK cool bro awesome, btw a thing i noticed as i have started spending more time in dsa is that some days my brain works great n some days it doesn't work at all and i cant solve even simple stuff, is there any fix to this or is it kind of burnout?
That’s totally totally fine. It happens.
I always suggest to give oneself a break to relax and enjoy.
May be Sundays , keep it free to relax and enjoy a little. And in other days, work harder like no one can ever beat you 💪💪💪
@@codestorywithMIK thats the josh🔥but i have so much left to complete that i feel bad for taking time off 🙂
Brute Force approach using include/exclude pattern
const isValid = (temp: number[], target: number) => {
const min = Math.min(...temp);
const max = Math.max(...temp);
return max + min {
const dfs = (i: number, temp: number[]): number => {
if (i >= array.length) {
if (isValid(temp, target)) {
return 1;
} else {
return 0;
}
} else {
temp.push(array[i]);
const take = dfs(i + 1, temp);
temp.pop();
const doNotTake = dfs(i + 1, temp);
return take + doNotTake;
}
};
return dfs(0, []);
};
@codestorywithMIK, can we solve this problem with DP?
❤
we can also use binaryExponentiation for that pow() case
Thanks for sharing ❤️❤️
Would you please share any good resource from where I can read the documentation for this method also
@@codestorywithMIK ruclips.net/video/J63JPxqmyzg/видео.html
have you tried to use binaryExponentiation i feel that we can't use it as it will just increase the complexity.... and Math.pow is O(1) function and outputs the same thing that binaryExponentation will give... If you were able to make it work?? can you send me the code... because issue was that pow was too big....
@@architchanana7866 yes it will increase the time complexity by factor of O(logn).... So no need to go for that
Stick with the simple approach
@@architchanana7866 It Worked for me
Here's the code:
class Solution {
private:
const int M = 1e9+7;
int binExp(int a, int b)
{
int ans = 1;
while(b)
{
if(b&1)
{
ans = (ans * 1LL * a) % M;
}
a = (a * 1LL * a) % M;
b >>= 1;
}
return ans;
}
public:
int numSubseq(vector& nums, int target) {
sort(begin(nums), end(nums));
int n = nums.size();
int s = 0, e = n-1;
int ans = 0;
while(s target)
{
e--;
}
else
{
ans = (ans + binExp(2, e-s)) % M;
s++;
}
}
return ans;
}
};
As always ❤
Thank you so much vineet ❤️
Can we solve this problem using binary search?
Indeed yes.
I will try to make a video on that as well
Thank you
Hi MIK ❤ Aaj ka Leetcode challange question daalne vale ho?
Hi Hiren. Yes it will come today.
Just need some time. Little occupied in medications and hospitality
@@codestorywithMIK 😊❣️Get well soon MIK 💕
I used the same code without modulo and with pow() function , but i got wrong answer instead of runtime , below is the code , It is very helpful if you able to told where my code went wrong ??
int numSubseq(vector& nums, int target) {
sort(nums.begin(), nums.end());
long long int out = 0;
int i=0,j=nums.size()-1;
while(i
Java Code:
class Solution {
public int numSubseq(int[] nums, int target)
{
Arrays.sort(nums);
int l=0;
int r=nums.length - 1;
int mod=1000000007;
int power[]=new int[nums.length];
power[0]=1;
for(int i=1;i
Thanks a lot for Java ❤️
@@codestorywithMIK Thank you for explaining so beautifully 😁
Means a lot
Aur bhai, gala thik?
Today was my last doctors appointment.
It’s almost better now.
@@codestorywithMIK precompute kyu krna, binpow use kr lijiye 😅