… 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
could you make the following changes …
make the pref array of size n+1…
and pref[0] = 0;
and the for loop … for(int i=1;i<=n;i++) pref[i] = (pref[i-1]+arr[i-1])%k
and the nxt for loop … the one after the hash map … for(int i=0;i<=n;i++) …
also take sum as long long or any big int in your case
yes all tc broooo as i said. Div 1 are the greatest
one small doubt bro.
for(int i=0;i<=n;i++)
{
long reqd=(pref[i]-m%k+k)%k;
if(mp.containsKey(reqd))
{
mn=Math.min(mn,i+1-mp.get(reqd));
}
mp.put(pref[i],i+1);
}
Can you explain this part?
1.we take the prefix sum and why (pref[i]-m%k+k)%k?
2. If it exists, why minwith i+1-mp[reqd]?
3. why mp[pref[i]]=i+1?
only these 3 doubts bro. thank you so much for your time