subarray sum equals k leetcode | subarray sum equal to k python | amazon interview question

Sdílet
Vložit
  • čas přidán 21. 04. 2020
  • ✅ Best book for coding interview - amzn.to/3F3FW8q
    ✅ Please subscribe to our second channel - / @thecodingworldpodcast659
    Please like the video, this really motivates us to make more such videos and helps us to grow. thecodingworld is a community which is formed to help fellow student and professionals looking to crack the “coding interviews”.
    We love solving and sharing problems which are frequently asked in technical interviews and voted by community at various websites.
    Our website: thecodingworld.com/
    ✅ For more and upcoming updates join our community
    DISCORD - / discord
    TELEGRAM - t.me/joinchat/AAAAAEi9eHNAeJI...
    INSTAGRAM - thecodingworld?...
    FACEBOOK - / 50214. .
    Email - info.thecodingworld@gmail.com
    ✅ Struggling solving leetcode question give yourself a 5 min break ➡️➡️ • Video
    #leetcode #python

Komentáře • 62

  • @SmoothCode
    @SmoothCode Před rokem +9

    You did a better job at explaining than NeetCode did for this question. Well done.

  • @amoghmulge
    @amoghmulge Před 4 lety +39

    rather than explaining what you did , u must explain " why u did that". because we don't get the intuition or the idea behind it.

    • @allan1827
      @allan1827 Před 3 lety

      yep

    • @greatmono
      @greatmono Před 2 lety

      I agree, the video creator jumps into the solution directly. Any tom, dick and harry can do so. Waste of time.]

    • @nwokolobueze7291
      @nwokolobueze7291 Před 2 lety

      I think the video creator explain himself very clearly based on the question that was asked. What you should be asking is why was this question asked in the first place. This video was very helpful in understanding the concept involved in solving the question (IMO).

    • @_sayandas
      @_sayandas Před 3 měsíci +1

      @@nwokolobueze7291 Did you even watch the video? He said "So what will be an optimized approach" and then straight went to "Let's take cumulative sum" and then straight to subtracting them. Bruh why should we use cumulative sum specifically and not any other approach, what is the intuition behind it we need to know that right. He didnt explain that and applied directly because he saw it somewhere else and memorized it. And we learned the same, to memorize it. There's no point in memorizing a specific approach to a specific question

  • @kumarshivam5453
    @kumarshivam5453 Před 4 lety +6

    seen so many other videos on the same problem, but the explanation that u give is exemplary, loved the explanation

  • @annjames6522
    @annjames6522 Před 3 lety +8

    Thank you ! I like the way you take the time, to first, explain the approach, before you begin coding.

  • @davidpaolochua7877
    @davidpaolochua7877 Před 3 lety +1

    best explanation of this problem I've come across with. Thank you! A lot of the other videos doesn't explain count += map.get(sum-k) part very well. But this is gold!

  • @appcolab
    @appcolab Před 2 lety +2

    By far the best approach and well explained

  • @petrob123
    @petrob123 Před 3 lety +3

    You are awesome. Struggled hella. Finally understood this algo.

  • @kingjohnkk
    @kingjohnkk Před 3 lety +2

    dude ur so smart... and ur explanation is so clear its like ASMR

  • @praison9104
    @praison9104 Před 4 lety +1

    Thanks for the explanation! I was having a hard time understanding it for a while but it's clear now!

    • @nwokolobueze7291
      @nwokolobueze7291 Před 2 lety

      Me too, That was why I came here in the first place. I was not understanding the question at first.

  • @shen-yusun7683
    @shen-yusun7683 Před 4 lety +1

    Thank you! Very easy to understand the explanation!

  • @sukesh0726
    @sukesh0726 Před 4 lety +1

    Perfect explanation! Great Job

  • @aditya4974
    @aditya4974 Před 4 lety +2

    thank you, that's a really nice solution! How can one build such intuition to apply math operations and think outside the box, rather than getting stuck at the layman approach of n*n time complexity?

  • @Shruthi_2797
    @Shruthi_2797 Před 5 měsíci

    Thank you so much , was breaking my head with neetcode

  • @mayankrathi505
    @mayankrathi505 Před 4 lety

    multidimensionality approach in your videos for answers is really good..

  • @cgqqqq
    @cgqqqq Před 2 lety +1

    you should increase value of the key only when the key shows up again in prefix sum array, but you are also increasing the value when the key shows up as diff here

  • @rahulaga
    @rahulaga Před 3 lety +1

    clean and clear explanation !!

  • @TheArbaaz-rn2tq
    @TheArbaaz-rn2tq Před 4 lety +1

    in line 15, why we are adding comulative sum to the dictonary by intial value 1 ? it could be any next indexed value

  • @gouthamkumar6749
    @gouthamkumar6749 Před rokem

    Keep the good work

  • @meetme2meat
    @meetme2meat Před 4 lety +2

    a) Why was the count for 14 was increased to 3
    b) Also, try this input
    [4,-2,2,1] with sum = 1 the output would be 2 whereas the actual output is only 1 i.e [-2,2,1]

    • @jasjotsingh9278
      @jasjotsingh9278 Před 4 lety +3

      i agree with you on "a", seems wrong. but for "b" {1} itself is also a subarray.

  • @jaganchowhaan9648
    @jaganchowhaan9648 Před 2 lety +1

    You're perfect.Just subscibed🤞

  • @SomnathDas-fg2qc
    @SomnathDas-fg2qc Před 4 lety

    Thank u bhaiya,for giving explaining it easy way,keep doing it

  • @arnabganguly4962
    @arnabganguly4962 Před 4 lety

    Nice explanation.Example array is good one.

  • @ashwanikumar8035
    @ashwanikumar8035 Před 4 lety

    This is really good work.

  • @jasonteng3858
    @jasonteng3858 Před 4 lety

    When I changed the sequence of if statements, it gave me an error. Why? Aren't they parallel?

  • @nwokolobueze7291
    @nwokolobueze7291 Před 2 lety +1

    Thanks for providing a solution to this problem. I have liked and subscribed. I hope that motivates you guys to do more.

    • @nwokolobueze7291
      @nwokolobueze7291 Před 2 lety

      Also, maybe someone else have asked this questions before, but why did you initialize n = len(nums) ? Because you did not use it in the code.

    • @nwokolobueze7291
      @nwokolobueze7291 Před 2 lety

      I just shared this video on leetcode discussion board, hopefully more users head over here to learn more

  • @sowndhariyanandarajkumar9132

    Please do more videos very good explanation !

  • @infinioda108
    @infinioda108 Před 3 lety +1

    sirji damn cool video respect++

  • @MayankSingh_is_me
    @MayankSingh_is_me Před 3 lety

    we don't really need to put {0:1} in hashmap initially, wen cumulative sum become 7, it would take care of {0:1}.
    great explanation though!!

  • @reemachourey9039
    @reemachourey9039 Před 4 lety

    Hi very nice explanation. Also which software do you use for drawing on screen.

  • @balajee41
    @balajee41 Před 3 lety

    If I have to return the subarray which contains elements, what would be the logic?

  • @atanuchowdhury6582
    @atanuchowdhury6582 Před 4 lety

    nicely done

  • @rohitshekharsingh2579
    @rohitshekharsingh2579 Před 3 lety

    ThankYou Parrot !!

  • @rahulsolankib
    @rahulsolankib Před 3 lety

    Ohhhhh freakkkk that was tricky damn!!! For those who are wondering about why do he incremented count when 13 occurs bcz 20-7 = 13 means if difference of curr sum - 7 == 13 that array between that is 7 and depending on how many time 13 sum is encountered we increment var count by that number

    • @genius1912
      @genius1912 Před 3 lety

      Please tell more about that
      I don't know

    • @Prat-zi1ou
      @Prat-zi1ou Před rokem

      Look I use ohhh and freekkkk i am cool

  • @sumitsarkar6581
    @sumitsarkar6581 Před 3 lety

    IN the last 3 elements in your example giving the sum of 7 then why we wrent counting that

  • @ployapinon6345
    @ployapinon6345 Před 2 lety

    Will you please explain why you get count equals 6? Your input to yield count = 4 not 6

  • @prabhatrai6292
    @prabhatrai6292 Před 4 lety +1

    but the count method explained has also time complexity O(n*n)

  • @Ishaana3290
    @Ishaana3290 Před rokem

    What is the meaning of count = count+ sumdict [s-k]

  • @Unbiased_News
    @Unbiased_News Před 3 lety +1

    Sliding window pattern can work?? Please tell me

  • @Ishaana3290
    @Ishaana3290 Před rokem

    And how the sum is added in dictionary

  • @m3hrdadmz
    @m3hrdadmz Před 2 lety

    Thanks for your clear explanation. However, your explanation is not valid for an array with elements [1, 2, 1, 3] and k = 3. This array has running (cumulative) sums of [1, 3, 4, 7] and based on your explanation only one contiguous subarrays exists: [1, 2], while it should be three: [1, 2], [2, 1], and [3] . According to your explanation, differences between each element in [1, 3, 4, 7] and k = 3 is [-2, 0, 1, 4], which are all new values in the hash table. Therefore, the hash table becomes {0:1, 1:1, 3:1, 4:1, 7:1}. Can you please tell me where I am wrong?

  • @madhavghai3286
    @madhavghai3286 Před 2 lety

    #IS MY APPROACH IS OK ?
    a = [int(x) for x in input().split()]
    k = int(input())
    ans = [1 for i in range(len(a)) for j in range(i+1,len(a)+1) if sum(a[i:j])==k]
    print(len(ans))

  • @ltorres321
    @ltorres321 Před 2 lety

    Has anyone tried this code its not working for me.

  • @shubhamtandan486
    @shubhamtandan486 Před 4 lety +1

    4:35 confused with 7-7 why this it should be 7-14 correct ?

    • @shwethaks7994
      @shwethaks7994 Před 4 lety +1

      Its sum - k value.. thats why it is 7 - 7..