Subarray Sum Equals K | Brute-Better-Optimal approach

preview_player
Показать описание
Lecture 40 : DSA Placement Series
Topic : Prefix Sum & Hashing
Problem : Leetcode 560

Want to study for Tech Placements/Internships from us :

Timestamps:
00:00 Introduction
00:32 Question Explanation
02:50 BRUTE Solution
08:03 Code
08:45 Time & Space Complexity
09:14 OPTIMAL Solution
29:40 Code
32:35 Time & Space Complexity

Shradha Ma'am community

#prefixsum #hashing
Рекомендации по теме
Комментарии
Автор

Feeling positive to be uploading lecture 40 of the series. I hope this DSA series is helping you in your learning journey.
Do let me know your favorite learnings after lecture completion, it keeps me motivated❤

ApnaCollegeOfficial
Автор

I'm really thankful to you. I've been trying to understand this for the last week, but no one explained it like you did.

lokeshkumar
Автор

Hello Apna College Team,

We can reduce few lines of code by implementing below. Isn't it ?

int subarraySum(vector<int>& nums, int k) {
int sum = 0, count = 0;

unordered_map<int, int> prefixSum;

for (int i = 0; i < nums.size(); i++) {
sum = sum + nums[i];

if (sum == k) {
count++;
}
int val = sum - k;
if (prefixSum.find(val) != prefixSum.end()) {
count = count + prefixSum[val];
}

prefixSum[sum]++;
}

return count;
}

iamhamza_m
Автор

please upload video regularly just a request

adityamishra
Автор

Best series in youtube ever.It seems like dsa is very easy if we study like this. Wonderfull mam think mam you don't left the microsoft it's god's choice.(Please upload video on daily basis there is a 3-5 days gap in the videos, i know you have a lot of work to do, but its my humble request.)

Harshitvarshneyv
Автор

have watched strivers video for this but didnt i understood thankyou!

Pranavkuruvella
Автор

Madam I am really so thankful to you I am just following your series although there is no such type of questions of DSA that is taught in our universities and I really thankful from the core of my heart

Maryam-bksz
Автор

I thought of a more better approach instead of making a separate vector for each particular prefix sum and then use a hash map upon the prefix vector, we can initialize a map with map[0] - 1 and then on the go check the prefix sum and insert it into the hash table, my solution is below :
class Solution {
public:
int subarraySum(vector<int>& vec, int target) {
unordered_map<int, int> map;
int arrSum = 0;
int counter = 0;
map[0] = 1; // For Cases where the first element is the target itself
for(int i=0;i<vec.size();i++){
arrSum += vec[i];
if(map.find(arrSum - target) != map.end()){
counter += map[arrSum-target]; //Incrementing the counter
}
map[arrSum]++; //Incrementing the frequency of the new prefix
}
return counter;
}
};
I hope this will clarify some confusion

piyushjain
Автор

I really enjoyed the way you explained. Keep up the amazing work! Looking forward to more lectures and insights. ❤

MuraliVardhan-ss
Автор

why we need concept "prefix sum", either we can solve it by sliding window concept ?

kunalchhipa
Автор

today i completed my lecture - 41 on 05-05-2025 at 23:34 pm

Aryan-ld
Автор

I am completely new to this channel. I am so happy to see more than 6M subscribers.
Your explanation is too good 👍.
Are you working anywhere ? or YT is your fulltime profession ?

RameshKumar-ngnf
Автор

Lecture 40 completed on 29 Nov 2024❤ Loved the concept of unoredered map

himanshukumar
Автор

mam🙋‍♂🙋‍♂if sum == k, then why to check sum of next sub array i.e., from i to j+1 obvious once we find sum = k then sum of next subarray = k+arr[j] which exceeds k ... can't we add break; after the line we find sum = k? to reduce complexity

VishwajeetMore-xlvm
Автор

Day 34 of DSA lecture 40 completed..9 June 2025

sadafkhan-xvjb
Автор

Shraddha mam kindly make a group where you take free mock interviews and peers also take interviews of each other if u guys in for this then kindly comment below

anshi_shish
Автор

Maam i am a arts student can i become a data scinecetice ??
Plz reply me ?😢😢

the___sajit
Автор

Thank you so much for this series... It's awesome!!!

chaitanyanaik
Автор

Please leetcode ka every day ka code discuss Kia karo na, ptod program ka please ❤

ঘুমকন্যা
Автор

Ma'am how to minimise the no of elements whose eventual sum is k not necessarily contiguous

RiteshChoudhary-di
welcome to shbcf.ru