SUBARRAY SUMS DIVISIBLE BY K | LEETCODE 974 | PYTHON SOLUTION

  Рет қаралды 2,967

Cracking FAANG

Cracking FAANG

Күн бұрын

Discord Link: / discord
Problem Link: leetcode.com/problems/subarra...
In this video we are solving an interesting question dealing with prefix sums: Subarray Sums Divisible by K (Leetcode 974).
This is really one of those questions where you've either solved enough prefix sum problems to understand how it works right away or you need to see the solution in order to get it. Figuring it out on your own is quite tricky as it's a bit hard to wrap your head around the logic with the remainders.

Пікірлер: 22
@anishpatro9222
@anishpatro9222 Ай бұрын
Thank u so much... Now I properly understood prefix sum topic
@firomsamt7642
@firomsamt7642 6 ай бұрын
man this problem almost made me cry thank you for the explanation great video
@crackfaang
@crackfaang 6 ай бұрын
Glad you found it useful. I really hate all the problems that are like "divisible by K", "subarray sum equals K"... they're simple to code but hard to understand
@firomsamt7642
@firomsamt7642 6 ай бұрын
@@crackfaang yeah those questions that involve math and geometry are really hard to figure out
@ivanzaplatar9033
@ivanzaplatar9033 11 ай бұрын
What helped me understand the motivation: Suppose we have two subarrays of sums, A, and B. Sum A has a remainder r and sum B doesn't have a remainder(divisible by k). We also know by the definition of divisibility that -- A = n1*k + r B = n2*k + 0 where n1 and n2 are some constants(not really important) adding them(pay attention to this part) A + B = n_1*k + r + n_2*k = (n_1 + n_2)*k + r WAIT THE REMAINDER IS THE SAME!! THIS HAS TO MEAN THAT ONE OF THEM DIDN'T HAVE A REMAINDER AND WAS DIVISIBLE BY K.
@sharkoshen
@sharkoshen Жыл бұрын
Very Clear and Helpful! Thanks a lot !
@FluteVJ
@FluteVJ Жыл бұрын
I've seen a lot of videos on this problem and this video is the best in explaining the solution. Thank you :)
@isma5627
@isma5627 Жыл бұрын
Wow, that is very nice explanation. Thank you man!
@rsKayiira
@rsKayiira Жыл бұрын
Great video!!
@djimgoudany
@djimgoudany Ай бұрын
Thanks great explanation
@venkatsaireddy1412
@venkatsaireddy1412 6 ай бұрын
Such a good example for the divisible by k (1,21).
@aquazod8366
@aquazod8366 5 ай бұрын
Thank you!
@AndrewCLC
@AndrewCLC Жыл бұрын
This was very useful for understanding the reasoning. thank you. I went through a bunch of other videos and your reasoning at 3:30-4:30 is what made it most useful to me. The "philosophical rant" was fine too, it's reassuring for those of us who couldn't figure it out from a cold start.
@AndrewCLC
@AndrewCLC Жыл бұрын
constructive criticism (and for anyone else coming to this that struggled) -- at 7:30 it gets a little confusing why you just added 3 to the total and didn't update the map. I reasoned this out: At -3, the remainder of 4 % 5 is 4. Map becomes {4=>4} Result is [-2,-3], [-2,-3,0], [-2,-3, 0, 5]. You add the current plus everything before. Result = 6. So if there was another X%5=4 down the line, it would be [x], [x,-2,-3], [x, -2, -3, 0] and [z, -2, -3, 0, 5],
@subee128
@subee128 6 ай бұрын
Thank you so much
@aaditya_87
@aaditya_87 9 ай бұрын
good shit brh
@katignaneshwar8647
@katignaneshwar8647 Жыл бұрын
Very good explanation bro
@khaleabhishek5023
@khaleabhishek5023 Жыл бұрын
amazing video only video i would prefer specially for this problem
@sarayarmohammadi3376
@sarayarmohammadi3376 7 ай бұрын
Using defultdict(int), every remainder that is calculated will be 'present' in the remainders defaultdict, and it will have a default value of 0 if it hasn't been encountered before. So the if statement on line 11 will be true for all the keys. right? Maybe we can remove the if and just keep "count += remainders[running_sum % k]"?
@estambuleno
@estambuleno 11 ай бұрын
Nice explanation. Thank you. The if statement is redundant though
@MinhNguyen-lz1pg
@MinhNguyen-lz1pg 11 ай бұрын
the question is so dangerous that the police has to warn us
@MrZiyak99
@MrZiyak99 Жыл бұрын
space would be O(k) cause the map would have max k elements since you're taking the modulo
LONGEST LINE OF CONSECUTIVE ONES | LEETCODE 562 | PYTHON DFS SOLUTION
12:59
Leetcode 974: Subarray Sums Divisible by K
21:28
Algorithms Casts
Рет қаралды 1,9 М.
MISS CIRCLE STUDENTS BULLY ME!
00:12
Andreas Eskander
Рет қаралды 20 МЛН
BEST MEETING POINT | LEETCODE 296 | PYTHON OPTIMAL SOLUTION
18:06
Cracking FAANG
Рет қаралды 1,7 М.
LARGEST PALINDROMIC NUMBER | LEETCODE 2384 | PYTHON SOLUTION
14:56
Cracking FAANG
Рет қаралды 1,6 М.
Subarray Sums Divisible by K | LeetCode 974 | C++, Java, Python
16:48
Knowledge Center
Рет қаралды 34 М.
Subarray Sums Divisible by K | LeetCode 974
6:03
C0de Sutra
Рет қаралды 1,9 М.
Subarray Sums Divisible by K - Leetcode 974 - Python
16:41
NeetCodeIO
Рет қаралды 13 М.
K-TH MISSING POSITIVE NUMBER | LEETCODE 1539 | PYTHON SOLUTION
11:00
Cracking FAANG
Рет қаралды 2,5 М.
Oh, wait, actually the best Wordle opener is not “crane”…
10:53
Ba Travel Smart Phone Charger
0:42
Tech Official
Рет қаралды 1,2 МЛН
Запрещенный Гаджет для Авто с aliexpress 2
0:50
Тимур Сидельников
Рет қаралды 1,1 МЛН
تجربة أغرب توصيلة شحن ضد القطع تماما
0:56
صدام العزي
Рет қаралды 64 МЛН
Сколько реально стоит ПК Величайшего?
0:37
ноутбуки от 7.900 в тг laptopshoptop
0:14
Ноутбуковая лавка
Рет қаралды 3,6 МЛН