25. Subarray Sum Equals K | Leetcode 560 | Array | Prefix Sum

preview_player
Показать описание
Time Complexity : O(n)
Space Complexity : O(n)


Please like, share and subscribe if you found the video useful. Feel free to ask in comments section if you have any doubts. :)

#DataStructuresAndAlgorithms
#SubarraySumEqualsK
#PrefixSum
#interviewpreparation
Subarray Sum Equals K solution
Subarray Sum Equals K Leetcode
Subarray Sum Equals K C++
Subarray Sum Equals K Java
Subarray Sum Equals K Python

🔥🔥🔥🔥👇👇👇

Checkout the series: 🔥🔥🔥
LIKE | SHARE | SUBSCRIBE 🔥🔥😊
Рекомендации по теме
Комментарии
Автор

At 1:45, we have one more subarray of sum 9, i.e 1, 3, 2, 3

intezaralam
Автор

the way you are explaining the problem is very good keep doing more

devendratumu
Автор

Oh god...I tried soln from so many channels but unable to understand....Finally you are back wth the solution

rishalirishu
Автор

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<int>& nums, int k) {

int sum = 0;
int count = 0;
map<int, int> 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;
}
};

alienx
Автор

Thanks a million.. this explanation solved lots of doubts

CodeWithSeenu
Автор

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. 😀

RajeshS-nj
Автор

Very nice explaination as usual, negative numbers in picture makes it little tricky.

amanbhadani
Автор

Example se best samaj me aya Didi
Congratulation to join Microsoft

suraj_patwa
Автор

good explanation!.
i also started doing daily challenges from 2feb

nimeshsingh
Автор

I have seen william lin using prefix sum in the google kickstart videos. nice to have it revised here.

lightyagami-rkmy
Автор

god this sum wasted around 3hrs and above it many yt videos made the knot complex. but ur video is life saver. thank u.

abhisheke
Автор

Thanks a lot mam ! You making difference, you providing values ✌🏻

prajwalurkude
Автор

after doing some questions i observed one thing that most of the questions on subarrays can be solved using maps with optimisation.

rohangangwar
Автор

Thanks for explaining sum-k is needed with help of y-k, things got clear at that point .Thanks Ayushi :)

ambikaarora
Автор

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!

vaidanshkukreja
Автор

🔥🔥, mam is this approach is kadane's algorithm ?

shadowaj
Автор

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.

himanshubarak
Автор

Mam could you explain the problem sum of distances in tree from leetcode ?

amanverma
Автор

Is this random problems you solved from leetcode or daily challenge?

mohdkhaleeq
visit shbcf.ru