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

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ม.ค. 2025

ความคิดเห็น • 176

  • @ShinAkuma
    @ShinAkuma 3 วันที่ผ่านมา

    Best explanation for this technique I have found. The diagram helped a lot. I was struggling to understand this one for days.

  • @falakhasija2813
    @falakhasija2813 3 ปีที่แล้ว +16

    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 2 ปีที่แล้ว

      can we solve this problem by kadanes algo?

  • @VivekChandra-vr7gb
    @VivekChandra-vr7gb ปีที่แล้ว +5

    Adding variations to the problem statement is absolutely marvellous 👍

  • @indiradas1280
    @indiradas1280 3 ปีที่แล้ว +9

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

  • @vinisawali
    @vinisawali 3 ปีที่แล้ว +7

    Nice to see practical implementation of hash map.

  • @darshansomani1110
    @darshansomani1110 3 ปีที่แล้ว +7

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

  • @ranchocomedy7785
    @ranchocomedy7785 2 ปีที่แล้ว +2

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

  • @chandrakanthsagar8886
    @chandrakanthsagar8886 2 ปีที่แล้ว +2

    Valuable information... extraordinary teacher.. 💥💥

  • @hawking3396
    @hawking3396 3 ปีที่แล้ว +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?

  • @kunalmishra6869
    @kunalmishra6869 3 ปีที่แล้ว +5

    Best lecture on Hashmap 😍

  • @ANKITKUMAR-wn1ub
    @ANKITKUMAR-wn1ub 3 ปีที่แล้ว +2

    Great explaination bhaiya 🙏🙏

  • @prannoyroy5312
    @prannoyroy5312 8 วันที่ผ่านมา +1

    well explained. Thanks!

  • @stupidbutcurious6206
    @stupidbutcurious6206 2 ปีที่แล้ว

    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

  • @ZSHADOW18
    @ZSHADOW18 3 ปีที่แล้ว +8

    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 ปีที่แล้ว +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 ปีที่แล้ว

    gajab bhaiya 🔥🔥 very helpful content

  • @stith_pragya
    @stith_pragya 2 ปีที่แล้ว

    Thank You So Much Anuj Bhaiya.................🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @blackhawk3168
    @blackhawk3168 2 ปีที่แล้ว +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.

  • @NAMANMARKHEDKAR
    @NAMANMARKHEDKAR ปีที่แล้ว

    Wow bhaiya , Great explaination :D
    Thankyou so much

  • @devendrakharatmal8924
    @devendrakharatmal8924 3 ปีที่แล้ว +1

    Well explain thanks bhaiya👍

  • @garvgoel1743
    @garvgoel1743 2 ปีที่แล้ว +1

    very helpful

  • @satvik0099
    @satvik0099 ปีที่แล้ว

    Best explanation sir 🎉❤

  • @aasheeshprem1238
    @aasheeshprem1238 ปีที่แล้ว

    well done, Anuj Bhaiya... thanks!

  • @debanjanguhathakurata5221
    @debanjanguhathakurata5221 2 ปีที่แล้ว

    Very Nicely Explained Sir.

  • @AyushSharma-zl7fw
    @AyushSharma-zl7fw 3 ปีที่แล้ว +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 🔥❤️

  • @ansjamil7521
    @ansjamil7521 ปีที่แล้ว

    Excellent Brother you made my day

  • @prakhargahlot9373
    @prakhargahlot9373 ปีที่แล้ว

    You explained it really well

  • @prikeshsingh5971
    @prikeshsingh5971 3 ปีที่แล้ว +3

    OSM 👍👍

  • @hypnotic_dude
    @hypnotic_dude ปีที่แล้ว

    Really helpful bhaiya !!!! ❤

  • @Learnprogramming-q7f
    @Learnprogramming-q7f 11 หลายเดือนก่อน +1

    Thank you Bhaiya

  • @Knowledgeduniya1432
    @Knowledgeduniya1432 2 ปีที่แล้ว +1

    Imp point 09:22

  • @arpanbhowmick847
    @arpanbhowmick847 3 ปีที่แล้ว +23

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

  • @siddharthdixit5009
    @siddharthdixit5009 2 ปีที่แล้ว

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

  • @chandrakantkamble3371
    @chandrakantkamble3371 2 ปีที่แล้ว

    thanks for explaining

  • @itzmanny8905
    @itzmanny8905 3 ปีที่แล้ว +6

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

  • @aravindsingh08
    @aravindsingh08 3 ปีที่แล้ว +6

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

  • @techcode752
    @techcode752 3 ปีที่แล้ว

    Thanks Bhaiya For This Efforts 😃😃😃😃

  • @chubbooo
    @chubbooo 2 ปีที่แล้ว

    thanks, it is really helpful

  • @gwalaniarun
    @gwalaniarun 2 ปีที่แล้ว

    Amazing video mate. Thank you so much.

  • @InvincibleMan99
    @InvincibleMan99 9 หลายเดือนก่อน

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

  • @tusharchuhaan7506
    @tusharchuhaan7506 3 ปีที่แล้ว +1

    yes it was helpful

  • @nayanasagar6062
    @nayanasagar6062 3 ปีที่แล้ว +9

    1st like❤️

  • @nikhiljain6431
    @nikhiljain6431 3 ปีที่แล้ว +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 2 ปีที่แล้ว

      any solution for sum =0 ?

  • @startupmindset7597
    @startupmindset7597 2 ปีที่แล้ว +2

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

  • @benkigera
    @benkigera 2 ปีที่แล้ว +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.

  • @buddhadebgreat
    @buddhadebgreat 3 ปีที่แล้ว

    Very informative video bhaiya

  • @Knowledgeduniya1432
    @Knowledgeduniya1432 2 ปีที่แล้ว +1

    First time smjh m hi nhi aaya 🤣
    Second time ek ek shabd samjh m aaaya♥️😁🙏🏼

  • @dhruvilsoni4900
    @dhruvilsoni4900 3 ปีที่แล้ว +22

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

    • @vipulchawla5466
      @vipulchawla5466 3 ปีที่แล้ว +8

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

    • @armed3719
      @armed3719 3 ปีที่แล้ว +9

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

    • @adee6467
      @adee6467 2 ปีที่แล้ว +1

      -ve elements ma nahi valid hoga

  • @adarshmishra6699
    @adarshmishra6699 3 ปีที่แล้ว +1

    Anuj bhaiya op 💯💯💯

  • @devanshprakash8354
    @devanshprakash8354 ปีที่แล้ว

    V nice explanation

  • @anshulgoel1940
    @anshulgoel1940 ปีที่แล้ว

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

  • @AnishKumarSingh-su1dg
    @AnishKumarSingh-su1dg 2 ปีที่แล้ว +1

    Helpful :)

  • @sijalfatima7043
    @sijalfatima7043 ปีที่แล้ว

    O Man you doing great 👍

  • @ashishkumarnaik4255
    @ashishkumarnaik4255 3 ปีที่แล้ว +4

    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)

    • @zealindia553
      @zealindia553 24 วันที่ผ่านมา

      Where is it inaccurate? Unable to find it.

  • @ineerajnishad
    @ineerajnishad 2 ปีที่แล้ว

    Maja aaya!💚

  • @mycreations6383
    @mycreations6383 2 ปีที่แล้ว

    we can also use 2 pointers technique

  • @Raj_Tanvar
    @Raj_Tanvar 3 ปีที่แล้ว +1

    Amazing ♥️

  • @bestdeal3385
    @bestdeal3385 3 ปีที่แล้ว

    best teacher 😊

  • @motivationking11
    @motivationking11 3 หลายเดือนก่อน

    bhaiya 500k aaj ho jayega, aap congratulations.!
    aap apna course mujhe de do na. plz wo java development wala

  • @ZSHADOW18
    @ZSHADOW18 3 ปีที่แล้ว +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 2 ปีที่แล้ว +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

  • @sharad3877
    @sharad3877 2 ปีที่แล้ว +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 2 ปีที่แล้ว +1

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

    • @aeroabrar_31
      @aeroabrar_31 2 ปีที่แล้ว +1

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

  • @chiragsaurav9414
    @chiragsaurav9414 2 ปีที่แล้ว

    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 2 ปีที่แล้ว

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

    • @ratankumar1399
      @ratankumar1399 2 ปีที่แล้ว

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

  • @hemanthkumar240
    @hemanthkumar240 3 ปีที่แล้ว

    Thankyou ...🙏

  • @mrsttechno5974
    @mrsttechno5974 ปีที่แล้ว

    nice understand

  • @LaveshGarg
    @LaveshGarg 3 ปีที่แล้ว

    YES, IT WAS HELPFUL

  • @YashGupta-zh8ch
    @YashGupta-zh8ch 3 ปีที่แล้ว

    Thanks man . This was so good!

  • @dudamneerajdattu1459
    @dudamneerajdattu1459 ปีที่แล้ว +2

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

  • @m.afnan2018
    @m.afnan2018 2 ปีที่แล้ว

    Maza aagya bhaiya,,,

  • @subhamdudheria9523
    @subhamdudheria9523 2 ปีที่แล้ว

    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

  • @643kanavguleria9
    @643kanavguleria9 2 ปีที่แล้ว +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 2 ปีที่แล้ว +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 2 ปีที่แล้ว +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 2 ปีที่แล้ว

      @@643kanavguleria9 Thanks man!!

  • @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

  • @aniketkirar1194
    @aniketkirar1194 2 ปีที่แล้ว +1

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

  • @princepatel9080
    @princepatel9080 2 ปีที่แล้ว

    Helpfull

  • @DK-js8cz
    @DK-js8cz 6 หลายเดือนก่อน

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

  • @SunnySingh-is2wm
    @SunnySingh-is2wm 3 ปีที่แล้ว

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

  • @aniketchaurasia2323
    @aniketchaurasia2323 3 ปีที่แล้ว +6

    1 st one to comment bhaiya

  • @pranaybahuguna5523
    @pranaybahuguna5523 2 ปีที่แล้ว

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

  • @gazzhere
    @gazzhere 3 ปีที่แล้ว

    THANKYOU SOOOOO MUCH BHAIYA

  • @parthmangalkar
    @parthmangalkar 2 ปีที่แล้ว

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

  • @anshul2675
    @anshul2675 3 ปีที่แล้ว +2

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

  • @akashjagtap1096
    @akashjagtap1096 2 ปีที่แล้ว

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

  • @kanagalashradhaa7398
    @kanagalashradhaa7398 2 ปีที่แล้ว

    Can we also solve this Kadane algorithm

  • @tausifahmad2007
    @tausifahmad2007 7 หลายเดือนก่อน

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

  • @sayantanacharya2040
    @sayantanacharya2040 2 ปีที่แล้ว

    Just wondering if this works in case of a zero element in the array?🤔

  • @shaikgousemujeeb269
    @shaikgousemujeeb269 2 ปีที่แล้ว

    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 2 ปีที่แล้ว

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

  • @ultrasourus9715
    @ultrasourus9715 2 ปีที่แล้ว +1

    whahh yaar internet ke fayede toh bhot hai

  • @nandiniachugatla932
    @nandiniachugatla932 ปีที่แล้ว

    why you used end as -1 not 0???

  • @harshk8609
    @harshk8609 3 ปีที่แล้ว +5

    Bhaiya dsa ki video jadli jaldi dal do🙂🙂🙂

  • @darklord1781
    @darklord1781 3 ปีที่แล้ว

    Sliding window laga sakte hai na ?

  • @priyanshujain862
    @priyanshujain862 3 ปีที่แล้ว +6

    How to write this code in c++

  • @tech_wizard9315
    @tech_wizard9315 3 ปีที่แล้ว +5

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

  • @syedtaslimkawser984
    @syedtaslimkawser984 2 ปีที่แล้ว

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

  • @bharatrav1764
    @bharatrav1764 2 ปีที่แล้ว

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

  • @nikhil2373
    @nikhil2373 2 ปีที่แล้ว

    How would be 2indexing

  • @manu-qf4oh
    @manu-qf4oh 3 ปีที่แล้ว

    Hi anuj..
    Grt explanations..i tried one example {2, 12, 5} sum =7
    Why it is returning false?

    • @GURU-lz1iw
      @GURU-lz1iw 2 ปีที่แล้ว

      Coz no subarray exist with sum 7

    • @GURU-lz1iw
      @GURU-lz1iw 2 ปีที่แล้ว

      See subarray is continuous elements present is a array....i guess u are confused between subarray and subsequence

  • @riturajsingh9744
    @riturajsingh9744 2 ปีที่แล้ว

    #thanks

  • @aniketchaurasia2323
    @aniketchaurasia2323 3 ปีที่แล้ว +3

    I am learning web development and having too much problems in making logics in JavaScript please give any advice

    • @raziahmad8779
      @raziahmad8779 3 ปีที่แล้ว +1

      You will have to solve some basics questions

    • @aniketchaurasia2323
      @aniketchaurasia2323 3 ปีที่แล้ว

      What kind of ques and wher am i gonna get these que

    • @aniketchaurasia2323
      @aniketchaurasia2323 3 ปีที่แล้ว

      @@raziahmad8779 please tell

  • @Knowledgeduniya1432
    @Knowledgeduniya1432 2 ปีที่แล้ว

    Typical th per logical th practice karta rahunga bhaiyaa

  • @chandnibhatia1211
    @chandnibhatia1211 2 ปีที่แล้ว

    Are your batches on un-academy still taking new enrollments.

    • @AnujBhaiya
      @AnujBhaiya  2 ปีที่แล้ว

      No, the enrolment has been closed.

    • @chandnibhatia1211
      @chandnibhatia1211 2 ปีที่แล้ว

      @@AnujBhaiya Any new batches coming up...

  • @raghavsharma4195
    @raghavsharma4195 3 ปีที่แล้ว

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

  • @sahibnoorsingh2432
    @sahibnoorsingh2432 ปีที่แล้ว

    n value is missing, n=a.length;