if sum is -5 and k = 5 then key is 0 but as curr < 0 key would be changed Hence one more condition required curr += nums[i]; int key = curr % k; if(curr < 0 && key != 0){ key += k; }
I understand how it's 4 Just some math that i hate: a = -1 // 5, how to do it? -1 / 5 = -0.2, round it to the floor. a = -1. -1 % 5(it's b, ok?) = -1(a) * 5(b) + x -1 % 5 = -5 + x. x is 4 because we need to find the x that will give the first number(-1) after plusing the x with the result -1(a) * 5(b) (-5) -1 = -5 + x 4 = x Sorry if I made a mistake, but I heard this solution how to solve mod(%) with negative numbers
But with regard to this difference, do you know why? Why does python have a different result to java, shouldn't the operation be implemented in the same and be agnostic from programming language ?
I have a two technical interviews on Wednesday and Thursday. Hopefully I clear them and get the job🤞 Edit - Dint get selected for next round. I'll keep leetcoding until I get another job
pretty much got this thanks to yesterdays leetcode daily problem and by figuring out the pattern of how the occurence of multiple remainders are contributing in final ans
For java programmers if remainder is -ve then just add k to it to make it positive same as python does and all will be right. if(rem < 0) rem = k + rem;
Hint: If you're not using python, don't forget to adjust the remainder to be a positive number in case the language you're using returns a negative remainder. if (remainder < 0) remainder += k;
woudlt an array actually have n! subarrays since your starting at every position and each time decrementing one so it would be like n * (n - 1) * (n - 2)... ?
@@ActionOrginal Because in python compiler if you modulo some number it gives a positive number like this -4%7 = 3 but in c++ we will get this -4%7 = -4 thats way we have to convert the negative numbers into positive number for the question to match the previous reminder and count it.
Because of the problem yesterday, 523, I watched the video of 560, but I still feel a little confused about 523. Will you explain and upload it? Even make it into shorts would be helpful.
you would need to check if the key is present in the dictionary to increase its count or else you must set the count to one first, or am i missing something ans=c=0 m={} m[0]=1 for i in v: ans+=i if ans%k in m: c+=m[ans%k] m[ans%k]+=1 else: m[ans%k]=1 return c
@@hesheid9159 does it mean that the logic of this solution is only for python? In other language, we need to adjust the remainder to positive by ourself?
-1%5 is 4 in python but in other languages, java c#, javascript it is equal to -1.
curr+=nums[i];
int key = curr % k;
if(curr
if sum is -5 and k = 5 then key is 0 but as curr < 0 key would be changed
Hence one more condition required
curr += nums[i];
int key = curr % k;
if(curr < 0 && key != 0){
key += k;
}
@@YashTech16 key won't be zero buddy. How would you make a sum divisible by 0
I understand how it's 4
Just some math that i hate:
a = -1 // 5, how to do it? -1 / 5 = -0.2, round it to the floor. a = -1.
-1 % 5(it's b, ok?) = -1(a) * 5(b) + x
-1 % 5 = -5 + x. x is 4 because we need to find the x that will give the first number(-1) after plusing the x with the result -1(a) * 5(b) (-5)
-1 = -5 + x
4 = x
Sorry if I made a mistake, but I heard this solution how to solve mod(%) with negative numbers
This works in typescript: `let rem = ((sum % k) + k) % k;`
But with regard to this difference, do you know why? Why does python have a different result to java, shouldn't the operation be implemented in the same and be agnostic from programming language ?
I have a two technical interviews on Wednesday and Thursday. Hopefully I clear them and get the job🤞
Edit - Dint get selected for next round.
I'll keep leetcoding until I get another job
Good luck buddy
goodluck, and comeback to this comment to tell us how you did.
Good luck you got this!!!
Hopefully I get this question
You guys are getting interviews?
Pattern is same as yesterday problem.
Thanks man your explanation helped me solve this on my own.
Thanks for uploading this, the issue I realized I was having with this problem was due to negative numbers being modded in Java
Thank you, was able to write a working solution after your explanation
Hey @Neetcode
Can you please upload solutions for contests as well. It would be really helpful😊
much easier to understand than the leetcode editorial! Nice job!
pretty much got this thanks to yesterdays leetcode daily problem and by figuring out the pattern of how the occurence of multiple remainders are contributing in final ans
got this on an OA, now i dont feel so bad about not figuring this out in 20 mins
Main Point : If two prefix sums have the same remainder when divided by 𝐾 ,the subarray between these two prefix sums is divisible by 𝐾
this channel is so good
For java programmers if remainder is -ve then just add k to it to make it positive same as python does and all will be right. if(rem < 0) rem = k + rem;
That is a great explanation thanks a lot but coming up with it during an interview is another challenge altogether!!
For whoever stuck on "wrong" remainders for negative numbers - use Euclidean remainder instead of default % operator. It goes like
(n % m + m) % m.
Hey can you also solve the contest questions ❓
Great explanation as always. Thank you
Crazy intuition fr!
Hint: If you're not using python, don't forget to adjust the remainder to be a positive number in case the language you're using returns a negative remainder. if (remainder < 0) remainder += k;
I didn't get it. In this particular case divisible doesn't mean evenly divisible, i.e. it doesn't have to be prefix_sum % k == 0?
i always stuck on these kind of problems why i dont get a intution of solving them , I mean optimize solutions.
Ok I’m too addicted to these videos.. I need to stop.. this is past my bed time lolz
New testcases got added and this fails in case remainder is negative, so just add an offset in case remainder is negative to remainder+k
Will it work for negative remainder? For me its nor working.
yaa it doesn't work for me too
woudlt an array actually have n! subarrays since your starting at every position and each time decrementing one so it would be like n * (n - 1) * (n - 2)... ?
I think it would be n + (n-1) + (n-2) .. so on
Can you explain why (remainder < 0) remainder = k+remainder works?
Read about modular arithmetic for negative numbers.
Yayyy you’re finally back
bro what’s ur routine like
This is in python that's way we don't have to do this
int remain = (prefix_sum % k + k) % k;
Thanks man. btw why did it not work in c++ as it works fine in python
@@ActionOrginal Because in python compiler if you modulo some number it gives a positive number like this
-4%7 = 3 but in c++ we will get this -4%7 = -4 thats way we have to convert the negative numbers into positive number for the question to match the previous reminder and count it.
thank you neetcode!
For negative remainder, its not working.
Because of the problem yesterday, 523, I watched the video of 560, but I still feel a little confused about 523. Will you explain and upload it? Even make it into shorts would be helpful.
He already did it th-cam.com/video/OKcrLfR-8mE/w-d-xo.html
Bro, Your teaching is nice. It would be nicer if you took care of your accent. Please do
you would need to check if the key is present in the dictionary to increase its count or else you must set the count to one first, or am i missing something
ans=c=0
m={}
m[0]=1
for i in v:
ans+=i
if ans%k in m:
c+=m[ans%k]
m[ans%k]+=1
else:
m[ans%k]=1
return c
why we don't need check the remainder is positive?
python take right remainder we dont need to check it
@@hesheid9159 does it mean that the logic of this solution is only for python?
In other language, we need to adjust the remainder to positive by ourself?
@@zz-yy-xx yes we need another check if remainder is negative
Nice!!😊
thanks!
wrong also too it will yield less count as it is only checking the minimum no.to be subtracted but remainder +k remainder+2k and goes on
Am I stupid or this problem was hard? prolly I am stupid 😅
its a big math gap. i couldnt do it either.
I still don't understand it
Bob ross reference
I'm like a monkey being taught
P.s I didn't understand an explanation
first
Third
Forth