25. Subarray Sum Equals K | Leetcode 560 | Array | Prefix Sum

Поделиться
HTML-код
  • Опубликовано: 13 янв 2025

Комментарии • 60

  • @intezaralam9357
    @intezaralam9357 2 года назад +1

    At 1:45 , we have one more subarray of sum 9, i.e 1, 3, 2, 3

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  2 года назад +3

      Yes, that I missed, in later part of video, we did found out. Pinning your comment so that others can know. Thanks :)

  • @devendratumu1704
    @devendratumu1704 2 года назад +4

    the way you are explaining the problem is very good keep doing more

  • @rishalirishu5833
    @rishalirishu5833 2 года назад +3

    Oh god...I tried soln from so many channels but unable to understand....Finally you are back wth the solution

  • @rohangangwar6604
    @rohangangwar6604 2 года назад +2

    after doing some questions i observed one thing that most of the questions on subarrays can be solved using maps with optimisation.

  • @RajeshS-n2j
    @RajeshS-n2j 3 месяца назад

    like the reasoning and how you explained why sliding window and other 2 pointer approach will not work in this case, thanks for pointing it out. nice video. 😀

  • @CodeWithSeenu
    @CodeWithSeenu 3 месяца назад

    Thanks a million.. this explanation solved lots of doubts

  • @alienx2367
    @alienx2367 2 года назад

    Initial I thought of doing by sliding window later realised it consists of negative integers , This is a good approach also We can just do m[0] = 1 , so we don't have to if sum == k then count++
    class Solution {
    public:
    int subarraySum(vector& nums, int k) {
    int sum = 0;
    int count = 0;
    map Map;
    Map[0] = 1;
    for(int i = 0 ; i < nums.size() ; i++)
    {
    sum += nums[i];
    if(Map.find(sum-k) != Map.end())
    {
    count += Map[sum-k];
    }
    Map[sum]++;
    }
    return count;
    }
    };

  • @suraj_patwa
    @suraj_patwa 6 месяцев назад

    Example se best samaj me aya Didi
    Congratulation to join Microsoft

  • @amanbhadani8840
    @amanbhadani8840 2 года назад

    Very nice explaination as usual,negative numbers in picture makes it little tricky.

  • @nimeshsingh6229
    @nimeshsingh6229 2 года назад +1

    good explanation!.
    i also started doing daily challenges from 2feb

  • @lightyagami-rk2my
    @lightyagami-rk2my 2 года назад

    nice explanation thank you ma'am

  • @lightyagami-rk2my
    @lightyagami-rk2my 2 года назад

    I have seen william lin using prefix sum in the google kickstart videos. nice to have it revised here.

  • @prajwalurkude007
    @prajwalurkude007 2 года назад

    Thanks a lot mam ! You making difference, you providing values ✌🏻

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  2 года назад +1

      Welcome Prajwal, and thank you so much for appreciating. Means a lot :)

  • @rajdave7357
    @rajdave7357 2 года назад

    Thank you 🙏🙏

  • @ambikaarora8736
    @ambikaarora8736 2 года назад

    Thanks for explaining sum-k is needed with help of y-k, things got clear at that point .Thanks Ayushi :)

  • @abhisheke1927
    @abhisheke1927 3 месяца назад

    god this sum wasted around 3hrs and above it many yt videos made the knot complex. but ur video is life saver. thank u.

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  3 месяца назад

      thank you Abhishek, glad it was helpful 🤗🤗

  • @ecs185_shaileshbharti3
    @ecs185_shaileshbharti3 2 года назад +1

    💯🔥
    Reach ++;

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  2 года назад +1

      Thank you Shailesh for always supporting :)

  • @himanshubarak4944
    @himanshubarak4944 2 года назад

    I think instead of the last if else we can simply replace it by just using m[sum]++ in case the element is not present it will be same as setting m[sum]=1. Anyways loved the through explanation as always.

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  2 года назад

      Yes you are right, we can do that too :)
      Thanks :)

  • @vaidanshkukreja8970
    @vaidanshkukreja8970 2 года назад +1

    One little doubt 😅
    I think Space complexity will be o(1) as u r using an unordered_map and if not then please explain me coz i got little bit confused!

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  2 года назад +1

      No, Vaidansh, it will be O(n) , see how. In worst case, what might happen, is that at every index, whatever sum is till that index, we will store in unordered_map, so n elements means n sum i. e n keys in map

    • @vaidanshkukreja8970
      @vaidanshkukreja8970 2 года назад +1

      @@AyushiSharmaDSA ohh ya i got it now Thanks a lot for the explanation

  • @shadowaj9278
    @shadowaj9278 2 года назад

    🔥🔥, mam is this approach is kadane's algorithm ?

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  2 года назад

      No, this is not Kadane's algo :)

    • @shadowaj9278
      @shadowaj9278 2 года назад

      @@AyushiSharmaDSA mam can we apply kadane's in this question!

    • @vaidanshkukreja8970
      @vaidanshkukreja8970 2 года назад

      @@shadowaj9278 Yes i did apply kadane's algo but that didn't work out in some test cases so u have to go through either brute or this one and ya brute will give TLE!!

    • @shadowaj9278
      @shadowaj9278 2 года назад

      @@vaidanshkukreja8970 ok bro

  • @mohdkhaleeq7468
    @mohdkhaleeq7468 2 года назад

    Is this random problems you solved from leetcode or daily challenge?

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  2 года назад +1

      It's february leetcode daily challenge :)

    • @rajdave7357
      @rajdave7357 2 года назад

      @@AyushiSharmaDSA I think at the end you should also metione Day-10 Feb 2022 in the title of video, so that it will help us and also it will be helpful from SEO point of you : )

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  2 года назад

      @@rajdave7357 okay, thanks for letting me know :)

  • @amanverma126
    @amanverma126 2 года назад

    Mam could you explain the problem sum of distances in tree from leetcode ?

  • @SavitarOP
    @SavitarOP 2 года назад

    OP Solution

  • @sakshamsengar9798
    @sakshamsengar9798 2 года назад

    HAPPY TEDDY DAY🧸🧸🧸

  • @karthikruthiravelu8315
    @karthikruthiravelu8315 2 года назад

    At ruclips.net/video/XjP2mQr98WQ/видео.html , you are saying that we can have anything as value for hashmap which is not true as we have to maintain the count of sum, so the reason why this will not work is that we can have negative values in the sub array and because of which the same sum could have occurred multiple times and we might miss counting the subsequent sub arrays that have summed up to the same value. So it's always necessary to maintain the count of sum in our hashmap.

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  2 года назад

      Yes, you are right, said there by mistake :)