Find Subarray with the given sum k đŸ”„| Hashmap in Java | DSA-One Course #28

SdĂ­let
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

Komentáƙe • 172

  • @tejassontakke8382
    @tejassontakke8382 Pƙed 3 lety +7

    Nice to see practical implementation of hash map.

  • @darshansomani1110
    @darshansomani1110 Pƙed 3 lety +7

    OP Logic Building!
    Able to think and code in correct way. Thanks a lot!

  • @indiradas1280
    @indiradas1280 Pƙed 3 lety +8

    Anuj Bhaiya ,You are the best educator for me🙏🙏💐
    Take love & respect from West Bengal 💙
    God bless you Bhaiya 😇

  • @hawking3396
    @hawking3396 Pƙed 3 lety +14

    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?

  • @falakhasija2813
    @falakhasija2813 Pƙed 2 lety +12

    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.

  • @VivekChandra-vr7gb
    @VivekChandra-vr7gb Pƙed rokem +4

    Adding variations to the problem statement is absolutely marvellous 👍

  • @kunalmishra6869
    @kunalmishra6869 Pƙed 3 lety +5

    Best lecture on Hashmap 😍

  • @chandrakanthsagar8886
    @chandrakanthsagar8886 Pƙed rokem +2

    Valuable information... extraordinary teacher.. đŸ’„đŸ’„

  • @ranchocomedy7785
    @ranchocomedy7785 Pƙed 2 lety +2

    Bhaiya aapke videos se bohot acche se samaj aaraha hai thnank you.

  • @gwalaniarun
    @gwalaniarun Pƙed 2 lety

    Amazing video mate. Thank you so much.

  • @blackhawk3168
    @blackhawk3168 Pƙed 2 lety +5

    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 Pƙed 3 lety +2

    Great explaination bhaiya 🙏🙏

  • @arpanbhowmick847
    @arpanbhowmick847 Pƙed 3 lety +23

    Bhaiya...it will be helpful if u provide some variation problems on every lecture...like u did here ( few more )❀

  • @stupidbutcurious6206
    @stupidbutcurious6206 Pƙed rokem

    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

  • @devendrakharatmal8924
    @devendrakharatmal8924 Pƙed 3 lety +1

    Well explain thanks bhaiya👍

  • @debanjanguhathakurata5221

    Very Nicely Explained Sir.

  • @YashGupta-zh8ch
    @YashGupta-zh8ch Pƙed 3 lety

    Thanks man . This was so good!

  • @prakhargahlot9373
    @prakhargahlot9373 Pƙed rokem

    You explained it really well

  • @aasheeshprem1238
    @aasheeshprem1238 Pƙed rokem

    well done, Anuj Bhaiya... thanks!

  • @buddhadebgreat
    @buddhadebgreat Pƙed 3 lety

    Very informative video bhaiya

  • @garvgoel1743
    @garvgoel1743 Pƙed rokem +1

    very helpful

  • @startupmindset7597
    @startupmindset7597 Pƙed rokem +2

    i spend my all day with anuj bhaiya, shradha didi, and geeksforgeeks

  • @chubbooo
    @chubbooo Pƙed rokem

    thanks, it is really helpful

  • @ansjamil7521
    @ansjamil7521 Pƙed 6 měsĂ­ci

    Excellent Brother you made my day

  • @NAMANMARKHEDKAR
    @NAMANMARKHEDKAR Pƙed rokem

    Wow bhaiya , Great explaination :D
    Thankyou so much

  • @stith_pragya
    @stith_pragya Pƙed rokem

    Thank You So Much Anuj Bhaiya.................đŸ™đŸ»đŸ™đŸ»đŸ™đŸ»đŸ™đŸ»đŸ™đŸ»đŸ™đŸ»

  • @chandrakantkamble3371
    @chandrakantkamble3371 Pƙed rokem

    thanks for explaining

  • @ZSHADOW18
    @ZSHADOW18 Pƙed 3 lety +8

    Also one single element is also part of the subarray, so the last element '5' is also part of the subarray;

    • @vikashkumarbhagat3001
      @vikashkumarbhagat3001 Pƙed 2 lety

      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 Pƙed 2 lety +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].

  • @TechWek1
    @TechWek1 Pƙed rokem

    gajab bhaiya đŸ”„đŸ”„ very helpful content

  • @nikhiljain6431
    @nikhiljain6431 Pƙed 2 lety +4

    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 Pƙed 2 lety

      any solution for sum =0 ?

  • @LaveshGarg
    @LaveshGarg Pƙed 3 lety

    YES, IT WAS HELPFUL

  • @techcode752
    @techcode752 Pƙed 3 lety

    Thanks Bhaiya For This Efforts 😃😃😃😃

  • @JGyanRaj
    @JGyanRaj Pƙed 3 lety +1

    Amazing ♄

  • @benkigera
    @benkigera Pƙed rokem +1

    I don't understand his language but i still passed my test because of his explanation that i couldn't find in English videos.

  • @siddharthdixit5009
    @siddharthdixit5009 Pƙed 2 lety

    Perfect I have gone thorugh multiple videos bit none of them as compared to your solution

  • @hemanthkumar240
    @hemanthkumar240 Pƙed 3 lety

    Thankyou ...🙏

  • @satvik0099
    @satvik0099 Pƙed 6 měsĂ­ci

    Best explanation sir đŸŽ‰â€

  • @aravindsingh08
    @aravindsingh08 Pƙed 3 lety +6

    bro this was bomb of a video. gold content really!

  • @hypnotic_dude
    @hypnotic_dude Pƙed rokem

    Really helpful bhaiya !!!! ❀

  • @harshk8609
    @harshk8609 Pƙed 3 lety +5

    Bhaiya dsa ki video jadli jaldi dal do🙂🙂🙂

  • @anshulgoel1940
    @anshulgoel1940 Pƙed rokem

    Another way of handling that special case is to add a entry in HashMap - map[sum] = -1

  • @user-tk2vg5jt3l
    @user-tk2vg5jt3l Pƙed 5 měsĂ­ci +1

    Thank you Bhaiya

  • @tusharchuhaan7506
    @tusharchuhaan7506 Pƙed 3 lety +1

    yes it was helpful

  • @bestdeal3385
    @bestdeal3385 Pƙed 2 lety

    best teacher 😊

  • @gazzhere
    @gazzhere Pƙed 3 lety

    THANKYOU SOOOOO MUCH BHAIYA

  • @InvincibleMan99
    @InvincibleMan99 Pƙed 3 měsĂ­ci

    Thanks
    There is one more variation, that you have to count the num of subarrays.

  • @prikeshsingh5971
    @prikeshsingh5971 Pƙed 3 lety +3

    OSM 👍👍

  • @AnishKumarSingh-su1dg
    @AnishKumarSingh-su1dg Pƙed 2 lety +1

    Helpful :)

  • @Knowledgeduniya1432
    @Knowledgeduniya1432 Pƙed 2 lety +1

    First time smjh m hi nhi aaya đŸ€Ł
    Second time ek ek shabd samjh m aaayaâ™„ïžđŸ˜đŸ™đŸŒ

  • @devanshprakash8354
    @devanshprakash8354 Pƙed rokem

    V nice explanation

  • @mrsttechno5974
    @mrsttechno5974 Pƙed 10 měsĂ­ci

    nice understand

  • @mycreations6383
    @mycreations6383 Pƙed rokem

    we can also use 2 pointers technique

  • @princepatel9080
    @princepatel9080 Pƙed 2 lety

    Helpfull

  • @Knowledgeduniya1432
    @Knowledgeduniya1432 Pƙed 2 lety +1

    Imp point 09:22

  • @anshul2675
    @anshul2675 Pƙed 2 lety +2

    I was stuck at this question in the past interview I wish I found it sooner 😑

  • @m.afnan2018
    @m.afnan2018 Pƙed rokem

    Maza aagya bhaiya,,,

  • @adarshmishra6699
    @adarshmishra6699 Pƙed 3 lety +1

    Anuj bhaiya op 💯💯💯

  • @raghavsharma4195
    @raghavsharma4195 Pƙed 3 lety

    Bhaiya plz any list or sheet to practice or follow for dsa after ur vids???

  • @sijalfatima7043
    @sijalfatima7043 Pƙed rokem

    O Man you doing great 👍

  • @ashishkumarnaik4255
    @ashishkumarnaik4255 Pƙed 2 lety +3

    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 Pƙed 2 lety

      ``` 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)

  • @AyushSharma-zl7fw
    @AyushSharma-zl7fw Pƙed 3 lety +14

    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 đŸ”„â€ïž

  • @sayantanacharya2040
    @sayantanacharya2040 Pƙed 2 lety

    Just wondering if this works in case of a zero element in the array?đŸ€”

  • @akashjagtap1096
    @akashjagtap1096 Pƙed 2 lety

    Just one correction,
    add map.put() in else block ->>
    else {
    map.put(currtSum, i);
    }

  • @nayanasagar6062
    @nayanasagar6062 Pƙed 3 lety +9

    1st like❀

  • @sharad3877
    @sharad3877 Pƙed 2 lety +1

    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 Pƙed rokem +1

      Can anyone please explain me ....
      In C++
      How to write thi line ....
      Start = map.get(cursum-sum)+1;

    • @aeroabrar_31
      @aeroabrar_31 Pƙed rokem +1

      @@whoasadkhan map[cursum-sum]+1;

  • @DK-js8cz
    @DK-js8cz Pƙed 24 dny

    I think we can do this sum using sliding window technique also.

  • @tech_wizard9315
    @tech_wizard9315 Pƙed 3 lety +5

    Please add questions list for each video of DSA course. Enough to crack tech giant's

  • @parthmangalkar
    @parthmangalkar Pƙed 2 lety

    Please can any1 tell me any other optimum approach which does not use hash map?

  • @ineerajnishad
    @ineerajnishad Pƙed 2 lety

    Maja aaya!💚

  • @dhruvilsoni4900
    @dhruvilsoni4900 Pƙed 3 lety +19

    We can also use the concept of Sliding Window to solve this problem without using extra Space in the 1st Problem.

    • @vipulchawla5466
      @vipulchawla5466 Pƙed 2 lety +6

      How can we use sliding window in case of -ve elements

    • @armed3719
      @armed3719 Pƙed 2 lety +8

      @@vipulchawla5466 yes I dont think we can use sliding window for -ve

    • @adee6467
      @adee6467 Pƙed rokem +1

      -ve elements ma nahi valid hoga

  • @abhishekarora437
    @abhishekarora437 Pƙed 3 lety

    Bhaiya...please increase frequency of lectures

  • @aniketkirar1194
    @aniketkirar1194 Pƙed 2 lety +1

    thank you bhaiya the way you explain logic it's so simple and good that anyone can get it❀❀❀

  • @SunnySingh-is2wm
    @SunnySingh-is2wm Pƙed 2 lety

    Sir subarray with sum equal to zero with three elements taken in pairs bhi isse ho jaega??

  • @dudamneerajdattu1459
    @dudamneerajdattu1459 Pƙed rokem +1

    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;
    }

  • @itzmanny8905
    @itzmanny8905 Pƙed 2 lety +5

    Brute force approach
    int arr1[]={10,5,6,1,8,5,2};
    for(int i=0;i

  • @aniketchaurasia2323
    @aniketchaurasia2323 Pƙed 3 lety +6

    1 st one to comment bhaiya

  • @ultrasourus9715
    @ultrasourus9715 Pƙed 2 lety +1

    whahh yaar internet ke fayede toh bhot hai

  • @kanagalashradhaa7398
    @kanagalashradhaa7398 Pƙed rokem

    Can we also solve this Kadane algorithm

  • @arpitnakrani8108
    @arpitnakrani8108 Pƙed 2 lety

    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

  • @syedtaslimkawser984
    @syedtaslimkawser984 Pƙed 2 lety

    3,2,3
    sum=6 ..
    its returning 0,-1;
    but actual answer must be 0,2

  • @swatidhull5367
    @swatidhull5367 Pƙed 3 lety +1

    bhai make video on flutter ad swift............
    please,.....................................

  • @chiragsaurav9414
    @chiragsaurav9414 Pƙed 2 lety

    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 Pƙed rokem

      Can anyone please explain me ....
      In C++
      How to write thi line ....
      Start = map.get(cursum-sum)+1;

    • @ratankumar1399
      @ratankumar1399 Pƙed rokem

      @@whoasadkhan hey you can write like this
      start=m[curr_sum-k]+1;

  • @himanshukhairajani
    @himanshukhairajani Pƙed 3 lety

    I wonder how can someone dislike this video !!
    such a knowledgeable content.

  • @darklord1781
    @darklord1781 Pƙed 3 lety

    Sliding window laga sakte hai na ?

  • @sahilgupta7001
    @sahilgupta7001 Pƙed rokem +1

    your code won't work for the largest subarray with sum equal to k, please can you check it

  • @shaikgousemujeeb269
    @shaikgousemujeeb269 Pƙed rokem

    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 Pƙed rokem

      Can anyone please explain me ....
      In C++
      How to write thi line ....
      Start = map.get(cursum-sum)+1;

  • @subhamdudheria9523
    @subhamdudheria9523 Pƙed rokem

    Instead of that special case of currsum-sum==0 you could simply add map.put(0,1) right before the loop

    • @ajay2552
      @ajay2552 Pƙed rokem

      you mean (0,-1)?

    • @subhamdudheria9523
      @subhamdudheria9523 Pƙed rokem

      @@ajay2552 Kuch bhi karo kya farak padta hai

    • @ajay2552
      @ajay2552 Pƙed rokem

      @@subhamdudheria9523 are you sure? Kyuki jab i+1 print hoga.. tab 0 print hona chaiye.. but 2 ho jaega

    • @subhamdudheria9523
      @subhamdudheria9523 Pƙed rokem

      @@ajay2552 Start aur end variable Lelemge

  • @ZSHADOW18
    @ZSHADOW18 Pƙed 2 lety +1

    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 Pƙed 2 lety +4

      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

  • @tausifahmad2007
    @tausifahmad2007 Pƙed měsĂ­cem

    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 ??

  • @mdshadabalam7788
    @mdshadabalam7788 Pƙed 3 lety

    bhai thora jaldi videos upload kr dea kro, kafi wait karna pdta hai

  • @Knowledgeduniya1432
    @Knowledgeduniya1432 Pƙed 2 lety

    Typical th per logical th practice karta rahunga bhaiyaa

  • @pranaybahuguna5523
    @pranaybahuguna5523 Pƙed 2 lety

    This one is not really working when the sum of the sub array is negative. Is there something I am missing?

  • @nikhil2373
    @nikhil2373 Pƙed rokem

    How would be 2indexing

  • @bharatrav1764
    @bharatrav1764 Pƙed 2 lety

    bhaiya 12.30 se 13.00 calculaton thoda mistake ho gya ...if change that part it is beneficial for absolute biginer

  • @riturajsingh9744
    @riturajsingh9744 Pƙed rokem

    #thanks

  • @sahibnoorsingh2432
    @sahibnoorsingh2432 Pƙed 6 měsĂ­ci

    n value is missing, n=a.length;

  • @StudyiMnimal1743
    @StudyiMnimal1743 Pƙed 3 lety

    yes

  • @ashishkumarshawhitchem2017

    for leetcode 560 what will be the changes

  • @nikhil_squats
    @nikhil_squats Pƙed 3 lety +2

    [1,-1,0]
    sum=0
    It will fail for this case

  • @643kanavguleria9
    @643kanavguleria9 Pƙed rokem +1

    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

    • @643kanavguleria9
      @643kanavguleria9 Pƙed rokem +1

      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

    • @643kanavguleria9
      @643kanavguleria9 Pƙed rokem +1

      my bad
      just do
      -------------------------
      if(!mp.containsKey((sum)))
      mp.put(sum,i);
      --------------------------
      while putting elements
      it will not update the keys values

    • @amangautam1779
      @amangautam1779 Pƙed rokem

      @@643kanavguleria9 Thanks man!!