Subarrays Sums Divisible by K (Leetcode 974) Algorithm Explained

แชร์
ฝัง
  • เผยแพร่เมื่อ 25 พ.ย. 2024

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

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

    The negative mod trick is main part to identify in this problem.. Thanks for explaining that 👍🏼

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

    Sir you are a legend. You make everything look so easy.

  • @thegreekgoat98
    @thegreekgoat98 5 หลายเดือนก่อน +4

    Greatest explanation to this problem.

    • @papalima737
      @papalima737 5 หลายเดือนก่อน +2

      Found a leetcode daily buddy here lol!

  • @Manish-ww3lg
    @Manish-ww3lg ปีที่แล้ว +3

    your repititive explanation is so amazing sir😍😍
    make things learned while watching the video🤩🤩🤩

  • @VikasGupta-ok9lh
    @VikasGupta-ok9lh ปีที่แล้ว

    Best possible explaination for this sum

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

    a lotttt better than any other solutions on YT

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

    Awesome 👍. Your explanation looks so good that I can code directly without any second thought. Thanks a lot bro 👏👍

  • @Elon-musk-007
    @Elon-musk-007 4 ปีที่แล้ว +1

    Mazza aagya Waah kaafi time ke baad 2 videos dekhi hai ek saath !!!! Keep going

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

    Thank you for simplified explanation!❤️

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

    Just 1 min into the video, liked the way you are explaining. LIKED the video.

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

    I just can not thank this guy enough :)

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

    Subscribed it... Pahla video dekhne par hi subscribe kar liye... Bhai kitne patience ke sath padhate ho... Its just awesome/amazing...More power to u brother.

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

      I am glad. Keep learning. Keep supporting. Your kind words are the kind of motivation that truly help me in making more and more content. Especially, these days, not everybody is generous with motivating anybody either. It means a lot

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

    Excellent Video, Great Explanation

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

    What a simplified and well explained solution

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

      Thankyou beta!
      I am glad you liked it. I hope that you are watching till the end and trying to understand what, how, and especially why of the problem.
      If you like our efforts, will you like to write a few words about us here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )

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

    You missed a case of if(rem==0) then also count++ becuse its the sum staring from first index and need to be checked seperately

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

    I am very happy that I have discovered this channel, but sad also why not earlier, Anyways now i think my placements prep is going to be OP :)

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

      You are placed?

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

      @@jobanpreetsingh2370 yep

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

      @@aisakyunhotahai8130 you prepared from Pepcoding?

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

      @@jobanpreetsingh2370 I solved problems from leetcode.
      For concepts and difficult problems I used to watch TH-cam videos from different channels, pepcoding was also one of them.
      So, I prepared not completely but partly from pepcoding

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

      @@aisakyunhotahai8130 If you don't mind, can you please tell us where you got placed?

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

    This is nice. Got the intuition

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

    Nice video keep on making such videos.

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

    Superb! Explanation sir.

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

    Great explanation ❤

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

    Amazing explanation sir

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

    sir garda uraa diye aap toh

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

    Great Explanation sir!

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

    Hello @pepcoding, can you please explain Leetcode 523? Its using HashMap but there is no good explaination.

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

    simple python solution: def subarraysDivByK(self, nums: List[int], k: int) -> int:
    reminder_freq = defaultdict(int)
    reminder_freq[0] = 1
    res = 0
    prefixsum = 0
    for n in nums:
    prefixsum += n
    reminder = prefixsum % k
    res += reminder_freq[reminder]
    reminder_freq[reminder] += 1
    return res

  • @BaljeetSingh-bx8yj
    @BaljeetSingh-bx8yj 3 ปีที่แล้ว +1

    Amazing explanation!

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

    sumeet sir is a legend 🔥🔥🔥🔥

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

    you're the best :)

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

    You are awesome. Sir.

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

    thank you so much

  • @abc-ym4zs
    @abc-ym4zs 2 ปีที่แล้ว

    good explanation sir if possible try to provide c++ solution

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

    Sir please pehle brute force solution discuss kra kro then optimal solution 🙏

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

    It would be better if you could explain how you came to conclusion that the remainders should be equal .

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

      He actually started the video with that

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

    thanks sir the solution was easy to understand and great but it was not able to pass all the test cases for the same question on coding ninja 😿

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

    didn't explain about the combinations of mod groups, rest explanation was amazing.

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

    I want to meet you in real life i just got a 7 lpa at rev thank you sir i have learnd a lot from you

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

      Glad to know. Sure, I would love to. Please DM me on Instagram

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

    Wow sir

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

    Done!

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

    why did you added 0,1 in the hashmap initially? Can you please explain that?

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

      Beta, I regret to inform you that, I won't be able to answer/solve the personal doubts of each and every student over here. For clearing your doubts, you can join our community on telegram - t.me/pepcoding.

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

      its for when we have a subarray sum divisible by k for first contiguous elements starting from first element
      like example : 5 2
      k = 7
      for this answer should be 1 but if u do not add 0,1 initially you will get 0

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

    #include
    int subArrayCount(vector &arr, int k) {
    map mpp;
    mpp[0]=1;
    int count=0,presum=0,rem=0;
    for(int i=0;i

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

    Mjja agaya sir

  • @AjitSharma-km6ev
    @AjitSharma-km6ev 3 ปีที่แล้ว

    Beautiful.

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

      Glad that you liked it.For better experience visit on nados.pepcoding.com
      Don't forget to follow us on Instagram instagram.com/pepcoding/

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

    but if zeroes is present create errror in that

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

    line no. 9. -> should be map.put(0,-1) instead of map.put(0,1) isn't it? please confirm. thanks.

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

    How are you drawing so fast? Using Mouse or a pencil?

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

    how do we develop the intuition for this kinda approaches

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

      practice

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

      by solving it beforehand hehehehehe

    • @AnkitSingh-hh8fx
      @AnkitSingh-hh8fx 2 ปีที่แล้ว

      there is same kind of question find on leetcode
      subarray sum k
      subarray sum 0 gfg
      subarray product k
      subarray divisible by k

  • @PritamKumar-mr5dv
    @PritamKumar-mr5dv 2 ปีที่แล้ว

    A case added when all element non zero if it contains zero ur code fails

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

    Sir I have a question,what to do if I also need to print the subarrays as well?how to do it with this method. We are not able to add different indexes in hashmap for a single prefix sum/remainder

    • @hetanshumalik6778
      @hetanshumalik6778 5 หลายเดือนก่อน

      Can be done easily (but non-trivial) using vector or vector. For each remainder there is a set associated with it, which contains the starting index (L-1). So while iterating over calculating prefix sum, we also iterate over indices present for that remainder, where `i` would represent the ending indices, so just iterate and get the subaray. But using prefix sums for this approach seems rather too non-trivial and unnecessary, instead just iterate over the subarr with O(N^2). For the approach that I just gave, in the worst case, we would have to print out all the subarrays, which would mean this problem itself is O(N^2), I don't think this would be asked anywhere

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

    Legend.

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

      We are glad you liked it. For best experience visit on nados.pepcoding.com

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

    let say if sum=-14 then also 7 add karenge ki 7 ka multiple becasue remainder positive bnana hai right??

    • @VivekYadav-zj9qd
      @VivekYadav-zj9qd 3 ปีที่แล้ว

      Remainder =0, so no need to add 7 or its multiple

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

    Nice

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

      keep motivating, keep learning and keep loving Pepcoding😊

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

    Why don't you take this in english?

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

    us

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

    i dont like the video explain in english

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

      i don't like you. i will not.