like the reasoning and how you explained why sliding window and other 2 pointer approach will not work in this case, thanks for pointing it out. nice video. 😀
Initial I thought of doing by sliding window later realised it consists of negative integers , This is a good approach also We can just do m[0] = 1 , so we don't have to if sum == k then count++ class Solution { public: int subarraySum(vector& nums, int k) { int sum = 0; int count = 0; map Map; Map[0] = 1; for(int i = 0 ; i < nums.size() ; i++) { sum += nums[i]; if(Map.find(sum-k) != Map.end()) { count += Map[sum-k]; } Map[sum]++; } return count; } };
I think instead of the last if else we can simply replace it by just using m[sum]++ in case the element is not present it will be same as setting m[sum]=1. Anyways loved the through explanation as always.
One little doubt 😅 I think Space complexity will be o(1) as u r using an unordered_map and if not then please explain me coz i got little bit confused!
No, Vaidansh, it will be O(n) , see how. In worst case, what might happen, is that at every index, whatever sum is till that index, we will store in unordered_map, so n elements means n sum i. e n keys in map
@@shadowaj9278 Yes i did apply kadane's algo but that didn't work out in some test cases so u have to go through either brute or this one and ya brute will give TLE!!
@@AyushiSharmaDSA I think at the end you should also metione Day-10 Feb 2022 in the title of video, so that it will help us and also it will be helpful from SEO point of you : )
At ruclips.net/video/XjP2mQr98WQ/видео.html , you are saying that we can have anything as value for hashmap which is not true as we have to maintain the count of sum, so the reason why this will not work is that we can have negative values in the sub array and because of which the same sum could have occurred multiple times and we might miss counting the subsequent sub arrays that have summed up to the same value. So it's always necessary to maintain the count of sum in our hashmap.
At 1:45 , we have one more subarray of sum 9, i.e 1, 3, 2, 3
Yes, that I missed, in later part of video, we did found out. Pinning your comment so that others can know. Thanks :)
the way you are explaining the problem is very good keep doing more
Thank you Devendra :)
Yes, She is awesome
Yes, she is excellent in explanation.
@@vaibhavgoel7750 @Raj dave thanks guys🥺😄
Oh god...I tried soln from so many channels but unable to understand....Finally you are back wth the solution
Thank you Rishali, glad it was helpful :)
after doing some questions i observed one thing that most of the questions on subarrays can be solved using maps with optimisation.
Yes :)
like the reasoning and how you explained why sliding window and other 2 pointer approach will not work in this case, thanks for pointing it out. nice video. 😀
Glad it was helpful! thank you :)
Thanks a million.. this explanation solved lots of doubts
welcome, glad it helped :)
Initial I thought of doing by sliding window later realised it consists of negative integers , This is a good approach also We can just do m[0] = 1 , so we don't have to if sum == k then count++
class Solution {
public:
int subarraySum(vector& nums, int k) {
int sum = 0;
int count = 0;
map Map;
Map[0] = 1;
for(int i = 0 ; i < nums.size() ; i++)
{
sum += nums[i];
if(Map.find(sum-k) != Map.end())
{
count += Map[sum-k];
}
Map[sum]++;
}
return count;
}
};
Yes, we can do m[0]=1 too :)
Example se best samaj me aya Didi
Congratulation to join Microsoft
Thanks a lot 😊
Very nice explaination as usual,negative numbers in picture makes it little tricky.
Thank you Aman, glad it was helpful :)
good explanation!.
i also started doing daily challenges from 2feb
Thanks Nimesh :)
nice explanation thank you ma'am
Welcome, glad it was helpful :)
I have seen william lin using prefix sum in the google kickstart videos. nice to have it revised here.
Glad it was helpful :)
Thanks a lot mam ! You making difference, you providing values ✌🏻
Welcome Prajwal, and thank you so much for appreciating. Means a lot :)
Thank you 🙏🙏
Welcome :) glad it was helpful
Thanks for explaining sum-k is needed with help of y-k, things got clear at that point .Thanks Ayushi :)
Welcome Ambika, glad it was helpful :)
god this sum wasted around 3hrs and above it many yt videos made the knot complex. but ur video is life saver. thank u.
thank you Abhishek, glad it was helpful 🤗🤗
💯🔥
Reach ++;
Thank you Shailesh for always supporting :)
I think instead of the last if else we can simply replace it by just using m[sum]++ in case the element is not present it will be same as setting m[sum]=1. Anyways loved the through explanation as always.
Yes you are right, we can do that too :)
Thanks :)
One little doubt 😅
I think Space complexity will be o(1) as u r using an unordered_map and if not then please explain me coz i got little bit confused!
No, Vaidansh, it will be O(n) , see how. In worst case, what might happen, is that at every index, whatever sum is till that index, we will store in unordered_map, so n elements means n sum i. e n keys in map
@@AyushiSharmaDSA ohh ya i got it now Thanks a lot for the explanation
🔥🔥, mam is this approach is kadane's algorithm ?
No, this is not Kadane's algo :)
@@AyushiSharmaDSA mam can we apply kadane's in this question!
@@shadowaj9278 Yes i did apply kadane's algo but that didn't work out in some test cases so u have to go through either brute or this one and ya brute will give TLE!!
@@vaidanshkukreja8970 ok bro
Is this random problems you solved from leetcode or daily challenge?
It's february leetcode daily challenge :)
@@AyushiSharmaDSA I think at the end you should also metione Day-10 Feb 2022 in the title of video, so that it will help us and also it will be helpful from SEO point of you : )
@@rajdave7357 okay, thanks for letting me know :)
Mam could you explain the problem sum of distances in tree from leetcode ?
Sure Aman, noted :)
OP Solution
Thank you Saurabh for always supporting :)
HAPPY TEDDY DAY🧸🧸🧸
At ruclips.net/video/XjP2mQr98WQ/видео.html , you are saying that we can have anything as value for hashmap which is not true as we have to maintain the count of sum, so the reason why this will not work is that we can have negative values in the sub array and because of which the same sum could have occurred multiple times and we might miss counting the subsequent sub arrays that have summed up to the same value. So it's always necessary to maintain the count of sum in our hashmap.
Yes, you are right, said there by mistake :)