Subarray Sum Equals k | LeetCode 30 day Challenge | Day 22 | (C++, Java, Python) | LeetCode #560

preview_player
Показать описание

**** Best Books For Data Structures & Algorithms for Interviews:**********
*****************************************************************************

LeetCode 30 day Challenge | Problem 22 | Subarray Sum Equals K | 22 April,
Facebook Coding Interview question,
google coding interview question,
leetcode,
Subarray sum equals k,
subarray sum = k,
subarray sum,

#Amazon #Facebook #CodingInterview #LeetCode #30DayChallenge #Google #SubArraySum
Рекомендации по теме
Комментарии
Автор

Don't forget to share your solutions here in the comments section. Thanks.

KnowledgeCenter
Автор

This is the best explanation I have seen on this problem so far. I loved how you use the check curr_sum == k to increase the count when the value is exactly K, instead of using a more common (but non-intuitive) approach of initializing the map with {0: 1}.
Keep up the good work!

GustavoCevalhos
Автор

Best explanation. Finally found someone who is able to explain properly instead of just mugging up.

ujjwalgupta
Автор

At 13:00 i have done with the problem :), thanks for explaining.

I was confused in scenario where key value is 2 or more but your line at 12:23 helps to alot. This helps to make make scenario without prefixes.

aditgulia
Автор

Thanks very much brother. This video is very helpful

KiranKumar-sbti
Автор

Great Explanation! Also, I really like the way you solve in all three programming languages.

SUSBZ
Автор

why we used hashmap to store the values , was it possible to do this using hashSet also, to find if
currSum-k exists then add the count.

TheVR
Автор

That was some Epic explanation there :D

anuabraham
Автор

Will it work for Sum = some negative number?

Shubham-nyce
Автор

Today, 1st time I couldn't get you, but explanations are great.

My Solution with complete explanation


int subarraySum(vector<int>& nums, int k) {
int cur_sum = 0, count = 0;
unordered_map<int, int> u_map; // maps previous sums upto given index to their frequency
u_map.insert(make_pair(0, 1)); // Empty array also a subarray with 0 sum

for(const auto& a : nums){
cur_sum += a; // Sum of

// Sum of = Sum of of v[0....i-1]
// k = Sum of of v[0....i-1]
// Sum of = Sum of

// So find number of previous sums with SUM=sum-k
if(u_map.find(cur_sum-k) != u_map.end()){
count += u_map[cur_sum - k];
}

// Add this sum to map
u_map[cur_sum]++;
}
return count;
}

rajvijen
Автор

I'm so dumb I still can't understand it :(

officialspock