I believe you are asking why we are not doing curr_sum = curr_sum - temp.back() The reason is, we are not passing curr_sum as reference, so when the recursion rolls back, the curr_sum will come back to it last value at that recursion point. Notice that we are passing temp by reference, so any change to it will be reflected to every recursive point and hence we need to undo it before rolling back to last recursion point. Why we are passing temp by reference (&) ? Because, passing it by copy will increase time complexity because every time i pass a parameter, a copy will be made if I don’t pass it by reference. Hope this helps
Just a gentle advise bhai If you could arrange the playlist videos in order of difficulty it would boost your views. Also like Raj sir has uploaded videos in order jisme har particular topic (i.e graphs.dp,linked list) ke playlist ka agla video pichle video se related ho so it would definitely help you boost your channel exponentially. Btw nice video bhai keep it up♥️
Hi there, Actually i follow this pattern in my Concepts Playlist. As of now I have 2 Concepts playlist 1) DP Concepts & Qns 2) Graph Concepts & Qns It’s in order. Every video is related to last video . Topic wise coverage. You can find their links in the Description I am currently working on concepts playlist of other topics too. Soon they are coming. Thank you so much for this precious suggestion and your kind words. ❤️❤️🙏🙏
Leetcode 1477. Find Two Non-overlapping Sub-arrays Each With Target Sum plase can you explaination of this problem..please i request you post a video on this problem sir..
Hi, please upload the recursion concept and questions I have been waiting for the last two please this is my humble request because I tried many recursion playlists I am still confused about recursion.
In that case, you will have to use the same approach we would do in an array to find any subarray having sum = k. You can use map. Find the code below : class Solution { public: map mp; void pathNums(TreeNode* root, int currSum, int sum, int &count) { if(!root) return; currSum += root->val; if(mp.count(currSum-sum)) { count += mp[currSum-sum]; } mp[currSum]++; pathNums(root->left, currSum, sum, count); pathNums(root->right, currSum, sum, count); mp[currSum]--; } int pathSum(TreeNode* root, int sum) { if(!root) return 0; mp.clear(); mp[0] = 1; int count = 0, currSum = 0; pathNums(root, currSum, sum, count); return count; } };
Bhaiya why not doing currSum-=temp.pop_back() may you please explain🙏
I believe you are asking why we are not doing
curr_sum = curr_sum - temp.back()
The reason is, we are not passing curr_sum as reference, so when the recursion rolls back, the curr_sum will come back to it last value at that recursion point.
Notice that we are passing temp by reference, so any change to it will be reflected to every recursive point and hence we need to undo it before rolling back to last recursion point.
Why we are passing temp by reference (&) ?
Because, passing it by copy will increase time complexity because every time i pass a parameter, a copy will be made if I don’t pass it by reference.
Hope this helps
@@codestorywithMIK I was also confused in this. Thanks for answering
@@codestorywithMIK thanku bhaiya 🙏
I think you are the creator of "story to code" concept. Never seen this unique style before
I am able to do these problems now because of you. Thank you from the bottom of my heart. From me and my entire friend circle.
Good explanation understood i have solved this same intuition thanks jai shree ram.
Thanks a lot bhaiya ❣🫡
Just a gentle advise bhai
If you could arrange the playlist videos in order of difficulty it would boost your views.
Also like Raj sir has uploaded videos in order jisme har particular topic (i.e graphs.dp,linked list) ke playlist ka agla video pichle video se related ho so it would definitely help you boost your channel exponentially.
Btw nice video bhai keep it up♥️
Hi there,
Actually i follow this pattern in my Concepts Playlist.
As of now I have 2 Concepts playlist
1) DP Concepts & Qns
2) Graph Concepts & Qns
It’s in order. Every video is related to last video . Topic wise coverage. You can find their links in the Description
I am currently working on concepts playlist of other topics too. Soon they are coming.
Thank you so much for this precious suggestion and your kind words. ❤️❤️🙏🙏
MIK, would you be able to make a video for Leetcode 322, Coin Change. Also, Please update the DP concepts playlist. Love your solutions. Thanks
❤
thankyou bhaiya
I have one question if we check for sum==TotalSum before adding value in current sum then it is giving multiple values whyy???
did you got the answer??? i have the same question
Jai Shree Ram
Partition Array Into Two Arrays to Minimize Sum Difference | Leetcode #2035
please make a video on this problem plzz
Leetcode 1477. Find Two Non-overlapping Sub-arrays Each With Target Sum plase can you explaination of this problem..please i request you post a video on this problem sir..
Hi, please upload the recursion concept and questions I have been waiting for the last two please this is my humble request because I tried many recursion playlists I am still confused about recursion.
sure. Coming in few days.
Waiting eagerly sir
what if we had to find the paths from any node to any node with sum k
In that case, you will have to use the same approach we would do in an array to find any subarray having sum = k.
You can use map.
Find the code below :
class Solution {
public:
map mp;
void pathNums(TreeNode* root, int currSum, int sum, int &count) {
if(!root)
return;
currSum += root->val;
if(mp.count(currSum-sum)) {
count += mp[currSum-sum];
}
mp[currSum]++;
pathNums(root->left, currSum, sum, count);
pathNums(root->right, currSum, sum, count);
mp[currSum]--;
}
int pathSum(TreeNode* root, int sum) {
if(!root)
return 0;
mp.clear();
mp[0] = 1;
int count = 0, currSum = 0;
pathNums(root, currSum, sum, count);
return count;
}
};
@@codestorywithMIK got it thanks !