Find Subarray with the given sum k đ„| Hashmap in Java | DSA-One Course #28
VloĆŸit
- Äas pĆidĂĄn 19. 06. 2021
- Hey guys, In this video, We're going to solve a popular HashMap Problem, Find the subarray with given sum.
Solving the problems from here: practice.geeksforgeeks.org/pr...
đ„ł Join our Telegram Community:
Telegram channel: telegram.me/realanujbhaiya
Telegram group: telegram.me/dsa_one
đ Follow me on:
Instagram: / anuj.kumar.sharma
Linkedin: / sharma-kumar-anuj
Twitter: / realanujbhaiya
đž Use coupon code ANUJBHAIYA on GeeksforGeeks to avail discounts on courses!
đ Complete DSA Playlist: âą DSA-One Course - The C...
Complete Android Development Playlist: âą Android Development Tu...
Hashtags:
#anujbhaiya #dsaone
Ignore these tags:
subarray with given sum
find subarray with given sum
subarray with given sum geeks for geeks
subarray with given sum geeksforgeeks
subarray with sum 0
subarray sum equals k
number of subarrays with sum equals k
subarray with given sum gfg
subarray with given sum c++
find subarray having given sum
subarray with given sum negative numbers
find the subarray with given sum
find the subarray having the given sum
find subarray with a given sum
hashmap
hashmap in java
java hashmap
hashmap java
how hashmap works
how hashmap works internally
hashmap vs hashtable
hashmap internals
java hashmap tutorial
hashmap internal working in java
design hashmap
hashmap internal implementation in java
design hashmap leetcode
hashmap java 8
hashmap interview questions
python hashmap
how hashmap works internally in java
code
what is hashmap in java
java hashmap explained
hashmap implementation
Nice to see practical implementation of hash map.
OP Logic Building!
Able to think and code in correct way. Thanks a lot!
Anuj Bhaiya ,You are the best educator for međđđ
Take love & respect from West Bengal đ
God bless you Bhaiya đ
Thanks anuj bhaiya. You are building a great cornerstone for students. In between how to learn kubernetes and how to start learning devops. Could you please tell about this?
Sir you teach the best!! I was stuck on this problem since past 2 days and now it's all clear. Thank you for this amazing content.
can we solve this problem by kadanes algo?
Adding variations to the problem statement is absolutely marvellous đ
Best lecture on Hashmap đ
Valuable information... extraordinary teacher.. đ„đ„
Bhaiya aapke videos se bohot acche se samaj aaraha hai thnank you.
Amazing video mate. Thank you so much.
Hey anuj. Thanks for the great content.
Tried doing the largest subarray with equal 0,1 problem as mentioned at the end of this video.
I think its not a variant to this one and would be solved with a different logic altogether.
As if we do find subarray with sum zero, the algorithm of this video will find the LAST subarray with sum zero and not the LARGEST subarray with sum zero.
If I'm right, please do cut the end part of this video.
Great explaination bhaiya đđ
Bhaiya...it will be helpful if u provide some variation problems on every lecture...like u did here ( few more )â€ïž
very well explained... i cam across this question while searching for another similar but a littile more complex... can you please help solve..
The Question is:- Given an array of integers of size N, count all possible distinct triplets whose sum is exactly divisible by given integer K. for triplets i, j, k --> i
Well explain thanks bhaiyađ
Very Nicely Explained Sir.
Thanks man . This was so good!
You explained it really well
well done, Anuj Bhaiya... thanks!
Very informative video bhaiya
very helpful
i spend my all day with anuj bhaiya, shradha didi, and geeksforgeeks
thanks, it is really helpful
Excellent Brother you made my day
Wow bhaiya , Great explaination :D
Thankyou so much
Thank You So Much Anuj Bhaiya.................đđ»đđ»đđ»đđ»đđ»đđ»
thanks for explaining
Also one single element is also part of the subarray, so the last element '5' is also part of the subarray;
There can be multiplie subarray but here we need to print any one
If question is find all possible subarray with given sum ,then your ans will also be included.
@@vikashkumarbhagat3001 But what he told will not work for the largest subarray. In the example the largest subarray with sum 5 would be [-5, 15, -10, 5], but if we do it according to what he said largest subarray is [15, -10].
gajab bhaiya đ„đ„ very helpful content
I faced many problems in this video, If there is only 1 element then it will not work or if the sum is 0 then every time currSum - sum will hold true because your sum is zero, this means you are basically checking if currSum is present or not, spoiler alert it's always present because you are putting currSum. Hope you see this and no hate towards you, what you are doing is very helpful for students like me
any solution for sum =0 ?
YES, IT WAS HELPFUL
Thanks Bhaiya For This Efforts đđđđ
Amazing â„ïž
I don't understand his language but i still passed my test because of his explanation that i couldn't find in English videos.
Perfect I have gone thorugh multiple videos bit none of them as compared to your solution
Thankyou ...đ
Best explanation sir đâ€
bro this was bomb of a video. gold content really!
Really helpful bhaiya !!!! â€
Bhaiya dsa ki video jadli jaldi dal dođđđ
Yesss
Haâ€
Another way of handling that special case is to add a entry in HashMap - map[sum] = -1
Thank you Bhaiya
yes it was helpful
best teacher đ
THANKYOU SOOOOO MUCH BHAIYA
Thanks
There is one more variation, that you have to count the num of subarrays.
OSM đđ
Helpful :)
First time smjh m hi nhi aaya đ€Ł
Second time ek ek shabd samjh m aaayaâ„ïžđđđŒ
V nice explanation
nice understand
we can also use 2 pointers technique
Helpfull
Imp point 09:22
I was stuck at this question in the past interview I wish I found it sooner đ
Maza aagya bhaiya,,,
Anuj bhaiya op đŻđŻđŻ
Bhaiya plz any list or sheet to practice or follow for dsa after ur vids???
O Man you doing great đ
i replaced the sum with 0 and transformed the array means replaced 0 with -1 and saw that the result is not accurate
input={1,1,0,1,1,0,0}
output - 3,6
Could you please check the code once @Anuj Bhaiya
``` Python: ```
arr = [1, 1, 0, 1, 1, 0, 0]
cur = maxi = 0
d = {0:-1}
for i in range(len(arr)):
if arr[i] == 1: cur += 1
else: cur -= 1
if cur in d:
maxi = max(maxi, i - d[cur] + 2)
d[cur] = i
print(maxi)
Bhaiya please focus and complete the DSA-One seriesđđ» make your DSA-One series, better than Apna College, content wise. Because their content is very vast and lots of different questions, but explanation is not good. You're the god of explaining thingsâ€ïžâ€ïž
Ps- till now your content of Dsa-One is đ„â€ïž
Ek dam sahi bola bhai
â€
wo scripted hai esliye đđ
Exactly @SURESH KUMAR đđđđđđ
Just wondering if this works in case of a zero element in the array?đ€
Just one correction,
add map.put() in else block ->>
else {
map.put(currtSum, i);
}
1st likeâ€ïž
in this given example, we have two sub arrays whose sum equals to 20. So I think we need to store all such pairs.
Can anyone please explain me ....
In C++
How to write thi line ....
Start = map.get(cursum-sum)+1;
@@whoasadkhan map[cursum-sum]+1;
I think we can do this sum using sliding window technique also.
Please add questions list for each video of DSA course. Enough to crack tech giant's
Please can any1 tell me any other optimum approach which does not use hash map?
Maja aaya!đ
We can also use the concept of Sliding Window to solve this problem without using extra Space in the 1st Problem.
How can we use sliding window in case of -ve elements
@@vipulchawla5466 yes I dont think we can use sliding window for -ve
-ve elements ma nahi valid hoga
Bhaiya...please increase frequency of lectures
thank you bhaiya the way you explain logic it's so simple and good that anyone can get itâ€ïžâ€ïžâ€ïž
Sir subarray with sum equal to zero with three elements taken in pairs bhi isse ho jaega??
my bruteforce approach (155/165 cases passed) :
vector subarraySum(vectorarr, int n, long long s)
{
// Your code here
vector v;
int sum = 0;
for(int i = 0; i < n; i++) {
for(int j = i; j < n; j++) {
sum += arr[j];
if(sum > s) {
break;
}
else if(sum == s) {
v.push_back(i + 1);
v.push_back(j + 1);
return v;
}
}
sum = 0;
}
v.push_back(-1);
return v;
}
the hashmap solution is showing TLE (133/165 cases passed), anyone correct me.
vector subarraySum(vectorarr, int n, long long s)
{
unordered_map u;
int sum = 0, index = 0;
for(int i = 0; i < n; i++) {
sum += arr[i];
u[sum] = index++;
if(u.count(s)) {
v.push_back(1);
v.push_back(i + 1);
break;
}
if(u.count(sum - s)) {
v.push_back(u[sum - s] + 2);
v.push_back(i + 1);
break;
}
}
if(v.size() == 0) {
v.push_back(-1);
return v;
}
return v;
}
Brute force approach
int arr1[]={10,5,6,1,8,5,2};
for(int i=0;i
Are bhai tu ekde....đ
1 st one to comment bhaiya
whahh yaar internet ke fayede toh bhot hai
Can we also solve this Kadane algorithm
not working in 0 & 1 ..
bcz if map contains repetive sum then if we want to update start then it will look for last inserted value
in our case we want first inserted value
3,2,3
sum=6 ..
its returning 0,-1;
but actual answer must be 0,2
bhai make video on flutter ad swift............
please,.....................................
We can't find all subarray by using 2 nested for loop as you mentioned at( 2:10). We have to use 3 nested for loop to find all subarray in brute force method.
Can anyone please explain me ....
In C++
How to write thi line ....
Start = map.get(cursum-sum)+1;
@@whoasadkhan hey you can write like this
start=m[curr_sum-k]+1;
I wonder how can someone dislike this video !!
such a knowledgeable content.
Sliding window laga sakte hai na ?
your code won't work for the largest subarray with sum equal to k, please can you check it
Nice explanation, what if I have 0 in the array value for the first question? In C# Dictionary keys should be unique right what if I found duplicate keys? will then also this algorithm work?
Can anyone please explain me ....
In C++
How to write thi line ....
Start = map.get(cursum-sum)+1;
Instead of that special case of currsum-sum==0 you could simply add map.put(0,1) right before the loop
you mean (0,-1)?
@@ajay2552 Kuch bhi karo kya farak padta hai
@@subhamdudheria9523 are you sure? Kyuki jab i+1 print hoga.. tab 0 print hona chaiye.. but 2 ho jaega
@@ajay2552 Start aur end variable Lelemge
I did't get one thing, why are we storing the curr sum as key and index as values in HashMap. Shouldn't we store the index as key and curr sum as values.
its key value pair
so if we have to set value which we want to retrive.
here we want index number as output so our index should be value and key will be currsum
I think this code will give the first subarray only, what if we have to find all subarray/number of subarray which is equal to given sum ??
bhai thora jaldi videos upload kr dea kro, kafi wait karna pdta hai
Typical th per logical th practice karta rahunga bhaiyaa
This one is not really working when the sum of the sub array is negative. Is there something I am missing?
How would be 2indexing
bhaiya 12.30 se 13.00 calculaton thoda mistake ho gya ...if change that part it is beneficial for absolute biginer
#thanks
n value is missing, n=a.length;
yes
for leetcode 560 what will be the changes
[1,-1,0]
sum=0
It will fail for this case
how to find the largest length , if map keys are(sum) and values are index, when the sum is repeated the index got updated and we can't get actual length when we find the first occurrence of cursum-sum we get updated index of cursum-sum
17 15
-13 0 6 15 16 2 15 -12 17 -16 0 -3 19 -3 2 -9 -6
for this test case k=15
my bad
just do
-------------------------
if(!mp.containsKey((sum)))
mp.put(sum,i);
--------------------------
while putting elements
it will not update the keys values
@@643kanavguleria9 Thanks man!!