I think the solution to the problem would be something like this:
Take the sum of the current array, if the sum is divisible by k then the answer would be 0;
else
now you basically want to subtract the value sum%k from the current sum to make it divisible by k.
For this you could have an array b ,where each element is
bi = (bi-1 + ai) % m, for all 1 <= i < n (0-based indexing) and b0 = a0, and m = sum%k.
You want a subarray which is a multiple of m, which basically now means , in array b,
(bi - bj)%m = 0, meaning bi%m = bj%m.
So now find in the array b same elements whose indices are the least apart, and you will have you answer.
My solution.
I am unsure if my solution is correct.
Bro
5 10
10 9 6 9 4
For this input, we can remove 2 sub arrays of size 1 (9 and 9) to make the sum divisible by 10.
your output is 3. you cannot remove any 3 sub array to make the sum divisible by 10.
how 3?
I am sorry if I have a wrong assumption. please clarify me. Thanks a lot for your time
im sorry , i was wrong, i’ll try to rectify and get back to you.
Also you need to remove only one subarray, otherwise the solution would always have been 1. so the answer to your testcase is 4 and not 1 , remove the subarray [9,6,9,4].
… im nowhere near … i think you’re being sarcastic but i have no way to tell.
Anyways , i think when i wrote “You want a subarray which is a multiple of m, which basically now means , in array b, (bi - bj)%m = 0, meaning bi%m = bj%m.”, I have no idea what i was thinking… its not actually the multiple of m but its something like
bi - bj = k.x + m, where x is a variable … anything from 0 to inf.
So now taking modulo on both sides by k i would get … (bi - bj )%k = (k.x + m)%k …
bi%k - bj%k = m%k. soln
If this doesn’t work sorry to waste your time … and let me know once you get the correct solution.
k is the number given to us n k a1,a2,a3 .... here.when we expand the modulo …
bi%k - bj%k = kx%k + m%k … now since kx%k would be 0 since kx is divisible by k