Find Subarray with the given sum k 🔥| Hashmap in Java | DSA-One Course #28

  Рет қаралды 179,822

Anuj Bhaiya

Anuj Bhaiya

3 жыл бұрын

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

Пікірлер: 171
@tejassontakke8382
@tejassontakke8382 3 жыл бұрын
Nice to see practical implementation of hash map.
@hawking3396
@hawking3396 3 жыл бұрын
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?
@darshansomani1110
@darshansomani1110 3 жыл бұрын
OP Logic Building! Able to think and code in correct way. Thanks a lot!
@tanmaykale6856
@tanmaykale6856 3 жыл бұрын
Sir Can you tell me how to start DSA for beginners. All resources.
@indiradas1280
@indiradas1280 3 жыл бұрын
Anuj Bhaiya ,You are the best educator for me🙏🙏💐 Take love & respect from West Bengal 💙 God bless you Bhaiya 😇
@VivekChandra-vr7gb
@VivekChandra-vr7gb Жыл бұрын
Adding variations to the problem statement is absolutely marvellous 👍
@falakhasija2813
@falakhasija2813 2 жыл бұрын
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.
@Dark2115azazel
@Dark2115azazel Жыл бұрын
can we solve this problem by kadanes algo?
@arpanbhowmick847
@arpanbhowmick847 3 жыл бұрын
Bhaiya...it will be helpful if u provide some variation problems on every lecture...like u did here ( few more )❤️
@kunalmishra6869
@kunalmishra6869 3 жыл бұрын
Best lecture on Hashmap 😍
@chandrakanthsagar8886
@chandrakanthsagar8886 Жыл бұрын
Valuable information... extraordinary teacher.. 💥💥
@ranchocomedy7785
@ranchocomedy7785 Жыл бұрын
Bhaiya aapke videos se bohot acche se samaj aaraha hai thnank you.
@gwalaniarun
@gwalaniarun 2 жыл бұрын
Amazing video mate. Thank you so much.
@blackhawk3168
@blackhawk3168 2 жыл бұрын
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.
@ANKITKUMAR-wn1ub
@ANKITKUMAR-wn1ub 3 жыл бұрын
Great explaination bhaiya 🙏🙏
@stupidbutcurious6206
@stupidbutcurious6206 Жыл бұрын
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
@debanjanguhathakurata5221
@debanjanguhathakurata5221 Жыл бұрын
Very Nicely Explained Sir.
@devendrakharatmal8924
@devendrakharatmal8924 3 жыл бұрын
Well explain thanks bhaiya👍
@prakhargahlot9373
@prakhargahlot9373 Жыл бұрын
You explained it really well
@aasheeshprem1238
@aasheeshprem1238 Жыл бұрын
well done, Anuj Bhaiya... thanks!
@stith_pragya
@stith_pragya Жыл бұрын
Thank You So Much Anuj Bhaiya.................🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@YashGupta-zh8ch
@YashGupta-zh8ch 2 жыл бұрын
Thanks man . This was so good!
@buddhadebgreat
@buddhadebgreat 3 жыл бұрын
Very informative video bhaiya
@ansjamil7521
@ansjamil7521 5 ай бұрын
Excellent Brother you made my day
@LaveshGarg
@LaveshGarg 3 жыл бұрын
YES, IT WAS HELPFUL
@NAMANMARKHEDKAR
@NAMANMARKHEDKAR 11 ай бұрын
Wow bhaiya , Great explaination :D Thankyou so much
@TechWek1
@TechWek1 Жыл бұрын
gajab bhaiya 🔥🔥 very helpful content
@startupmindset7597
@startupmindset7597 Жыл бұрын
i spend my all day with anuj bhaiya, shradha didi, and geeksforgeeks
@ZSHADOW18
@ZSHADOW18 2 жыл бұрын
Also one single element is also part of the subarray, so the last element '5' is also part of the subarray;
@vikashkumarbhagat3001
@vikashkumarbhagat3001 2 жыл бұрын
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.
@mohammedmustufa9928
@mohammedmustufa9928 2 жыл бұрын
@@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].
@JGyanRaj
@JGyanRaj 3 жыл бұрын
Amazing ♥️
@prikeshsingh5971
@prikeshsingh5971 3 жыл бұрын
OSM 👍👍
@user-tk2vg5jt3l
@user-tk2vg5jt3l 4 ай бұрын
Thank you Bhaiya
@chandrakantkamble3371
@chandrakantkamble3371 Жыл бұрын
thanks for explaining
@InvincibleMan99
@InvincibleMan99 2 ай бұрын
Thanks There is one more variation, that you have to count the num of subarrays.
@chubbooo
@chubbooo Жыл бұрын
thanks, it is really helpful
@techcode752
@techcode752 3 жыл бұрын
Thanks Bhaiya For This Efforts 😃😃😃😃
@bestdeal3385
@bestdeal3385 2 жыл бұрын
best teacher 😊
@benkigera
@benkigera Жыл бұрын
I don't understand his language but i still passed my test because of his explanation that i couldn't find in English videos.
@hemanthkumar240
@hemanthkumar240 3 жыл бұрын
Thankyou ...🙏
@satvik0099
@satvik0099 5 ай бұрын
Best explanation sir 🎉❤
@hypnotic_dude
@hypnotic_dude Жыл бұрын
Really helpful bhaiya !!!! ❤
@siddharthdixit5009
@siddharthdixit5009 2 жыл бұрын
Perfect I have gone thorugh multiple videos bit none of them as compared to your solution
@m.afnan2018
@m.afnan2018 Жыл бұрын
Maza aagya bhaiya,,,
@anshulgoel1940
@anshulgoel1940 Жыл бұрын
Another way of handling that special case is to add a entry in HashMap - map[sum] = -1
@gazzhere
@gazzhere 3 жыл бұрын
THANKYOU SOOOOO MUCH BHAIYA
@mrsttechno5974
@mrsttechno5974 9 ай бұрын
nice understand
@ineerajnishad
@ineerajnishad 2 жыл бұрын
Maja aaya!💚
@tusharchuhaan7506
@tusharchuhaan7506 2 жыл бұрын
yes it was helpful
@tech_wizard9315
@tech_wizard9315 3 жыл бұрын
Please add questions list for each video of DSA course. Enough to crack tech giant's
@devanshprakash8354
@devanshprakash8354 Жыл бұрын
V nice explanation
@garvgoel1743
@garvgoel1743 Жыл бұрын
very helpful
@AnishKumarSingh-su1dg
@AnishKumarSingh-su1dg Жыл бұрын
Helpful :)
@princepatel9080
@princepatel9080 2 жыл бұрын
Helpfull
@mycreations6383
@mycreations6383 Жыл бұрын
we can also use 2 pointers technique
@Knowledgeduniya1432
@Knowledgeduniya1432 Жыл бұрын
First time smjh m hi nhi aaya 🤣 Second time ek ek shabd samjh m aaaya♥️😁🙏🏼
@adarshmishra6699
@adarshmishra6699 3 жыл бұрын
Anuj bhaiya op 💯💯💯
@abhishekarora437
@abhishekarora437 3 жыл бұрын
Bhaiya...please increase frequency of lectures
@aravindsingh08
@aravindsingh08 3 жыл бұрын
bro this was bomb of a video. gold content really!
@sijalfatima7043
@sijalfatima7043 Жыл бұрын
O Man you doing great 👍
@ultrasourus9715
@ultrasourus9715 2 жыл бұрын
whahh yaar internet ke fayede toh bhot hai
@nayanasagar6062
@nayanasagar6062 3 жыл бұрын
1st like❤️
@anshul2675
@anshul2675 2 жыл бұрын
I was stuck at this question in the past interview I wish I found it sooner 😑
@AyushSharma-zl7fw
@AyushSharma-zl7fw 3 жыл бұрын
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 🔥❤️
@ANKITKUMAR-wn1ub
@ANKITKUMAR-wn1ub 3 жыл бұрын
Ek dam sahi bola bhai
@agyaani8060
@agyaani8060 3 жыл бұрын
@SURESHKUMAR-bq8ho
@SURESHKUMAR-bq8ho 3 жыл бұрын
wo scripted hai esliye 😁😁
@gauravgautam5963
@gauravgautam5963 2 жыл бұрын
Exactly @SURESH KUMAR 😂😂😂😂😂😂
@sayantanacharya2040
@sayantanacharya2040 Жыл бұрын
Just wondering if this works in case of a zero element in the array?🤔
@Knowledgeduniya1432
@Knowledgeduniya1432 Жыл бұрын
Imp point 09:22
@himanshukhairajani
@himanshukhairajani 2 жыл бұрын
I wonder how can someone dislike this video !! such a knowledgeable content.
@raghavsharma4195
@raghavsharma4195 3 жыл бұрын
Bhaiya plz any list or sheet to practice or follow for dsa after ur vids???
@aniketkirar1194
@aniketkirar1194 2 жыл бұрын
thank you bhaiya the way you explain logic it's so simple and good that anyone can get it❤️❤️❤️
@akashjagtap1096
@akashjagtap1096 2 жыл бұрын
Just one correction, add map.put() in else block ->> else { map.put(currtSum, i); }
@parthmangalkar
@parthmangalkar 2 жыл бұрын
Please can any1 tell me any other optimum approach which does not use hash map?
@nikhiljain6431
@nikhiljain6431 2 жыл бұрын
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
@10daysAgo
@10daysAgo Жыл бұрын
any solution for sum =0 ?
@sharad3877
@sharad3877 Жыл бұрын
in this given example, we have two sub arrays whose sum equals to 20. So I think we need to store all such pairs.
@whoasadkhan
@whoasadkhan Жыл бұрын
Can anyone please explain me .... In C++ How to write thi line .... Start = map.get(cursum-sum)+1;
@aeroabrar_31
@aeroabrar_31 Жыл бұрын
@@whoasadkhan map[cursum-sum]+1;
@aniketchaurasia2323
@aniketchaurasia2323 3 жыл бұрын
1 st one to comment bhaiya
@SunnySingh-is2wm
@SunnySingh-is2wm 2 жыл бұрын
Sir subarray with sum equal to zero with three elements taken in pairs bhi isse ho jaega??
@swatidhull5367
@swatidhull5367 3 жыл бұрын
bhai make video on flutter ad swift............ please,.....................................
@mdshadabalam7788
@mdshadabalam7788 3 жыл бұрын
bhai thora jaldi videos upload kr dea kro, kafi wait karna pdta hai
@itzmanny8905
@itzmanny8905 2 жыл бұрын
Brute force approach int arr1[]={10,5,6,1,8,5,2}; for(int i=0;i
@bhushankorg5606
@bhushankorg5606 2 жыл бұрын
Are bhai tu ekde....😁
@kanagalashradhaa7398
@kanagalashradhaa7398 Жыл бұрын
Can we also solve this Kadane algorithm
@harshk8609
@harshk8609 3 жыл бұрын
Bhaiya dsa ki video jadli jaldi dal do🙂🙂🙂
@manu-singh
@manu-singh 3 жыл бұрын
Yesss
@agyaani8060
@agyaani8060 3 жыл бұрын
Ha❤
@syedtaslimkawser984
@syedtaslimkawser984 2 жыл бұрын
3,2,3 sum=6 .. its returning 0,-1; but actual answer must be 0,2
@ECPrashantGoyal
@ECPrashantGoyal 2 жыл бұрын
tu cheez lajawab
@Knowledgeduniya1432
@Knowledgeduniya1432 Жыл бұрын
Typical th per logical th practice karta rahunga bhaiyaa
@tausifahmad2007
@tausifahmad2007 20 күн бұрын
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 ??
@ashishkumarnaik4255
@ashishkumarnaik4255 2 жыл бұрын
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
@mohammedmustufa9928
@mohammedmustufa9928 2 жыл бұрын
``` 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)
@sahilgupta7001
@sahilgupta7001 Жыл бұрын
your code won't work for the largest subarray with sum equal to k, please can you check it
@darklord1781
@darklord1781 2 жыл бұрын
Sliding window laga sakte hai na ?
@shresthgupta2670
@shresthgupta2670 2 ай бұрын
moj aa gai
@chinmaychaudhari4143
@chinmaychaudhari4143 3 жыл бұрын
Bhaiya java me pointers explain karva do
@shaikgousemujeeb269
@shaikgousemujeeb269 Жыл бұрын
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?
@whoasadkhan
@whoasadkhan Жыл бұрын
Can anyone please explain me .... In C++ How to write thi line .... Start = map.get(cursum-sum)+1;
@chiragsaurav9414
@chiragsaurav9414 Жыл бұрын
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.
@whoasadkhan
@whoasadkhan Жыл бұрын
Can anyone please explain me .... In C++ How to write thi line .... Start = map.get(cursum-sum)+1;
@ratankumar1399
@ratankumar1399 Жыл бұрын
@@whoasadkhan hey you can write like this start=m[curr_sum-k]+1;
@arpitnakrani8108
@arpitnakrani8108 2 жыл бұрын
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
@StudyiMnimal1743
@StudyiMnimal1743 3 жыл бұрын
yes
@sahibnoorsingh2432
@sahibnoorsingh2432 5 ай бұрын
n value is missing, n=a.length;
@riturajsingh9744
@riturajsingh9744 Жыл бұрын
#thanks
@ZSHADOW18
@ZSHADOW18 2 жыл бұрын
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.
@arpitnakrani8108
@arpitnakrani8108 2 жыл бұрын
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
@nikhil2373
@nikhil2373 Жыл бұрын
How would be 2indexing
@dudamneerajdattu1459
@dudamneerajdattu1459 Жыл бұрын
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; }
@subhamdudheria9523
@subhamdudheria9523 Жыл бұрын
Instead of that special case of currsum-sum==0 you could simply add map.put(0,1) right before the loop
@ajay2552
@ajay2552 Жыл бұрын
you mean (0,-1)?
@subhamdudheria9523
@subhamdudheria9523 Жыл бұрын
@@ajay2552 Kuch bhi karo kya farak padta hai
@ajay2552
@ajay2552 Жыл бұрын
@@subhamdudheria9523 are you sure? Kyuki jab i+1 print hoga.. tab 0 print hona chaiye.. but 2 ho jaega
@subhamdudheria9523
@subhamdudheria9523 Жыл бұрын
@@ajay2552 Start aur end variable Lelemge
@ashishkumarshawhitchem2017
@ashishkumarshawhitchem2017 Жыл бұрын
for leetcode 560 what will be the changes
@tulsidas2908
@tulsidas2908 2 жыл бұрын
Kaya Yaar bhaiya unordered map bhi padha dete CPP mei
@bharatrav1764
@bharatrav1764 Жыл бұрын
bhaiya 12.30 se 13.00 calculaton thoda mistake ho gya ...if change that part it is beneficial for absolute biginer
Каха ограбил банк
01:00
К-Media
Рет қаралды 10 МЛН
Tom & Jerry !! 😂😂
00:59
Tibo InShape
Рет қаралды 56 МЛН
Вечный ДВИГАТЕЛЬ!⚙️ #shorts
00:27
Гараж 54
Рет қаралды 9 МЛН
My little bro is funny😁  @artur-boy
00:18
Andrey Grechka
Рет қаралды 7 МЛН
Count Subarray sum Equals K | Brute - Better -Optimal
24:09
take U forward
Рет қаралды 220 М.
Subarray Sum Equals K - Prefix Sums - Leetcode 560 - Python
15:19
Find Sub Array Of An Array | FREE DSA Course in JAVA | Lecture 68
13:05
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 607 М.
Каха ограбил банк
01:00
К-Media
Рет қаралды 10 МЛН