We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
staticvoidMain(String[]args){varnk=Console.ReadLine().Split(' ').Select(Int32.Parse).ToArray();intn=nk[0];intk=nk[1];vararr=Console.ReadLine().Split(' ').Select(Int32.Parse).ToArray();varcount=0;varcounts=newint[k];for(vari=0;i<n;i++){// the idea is that if (a1 + a2) % k == 0// then (a1 % k + a2 % k) should be equal to k (or 0)varbucket=arr[i]%k;// adding all new combinations with arr[i] to the count// also handling bucket == 0 with % k herecount+=counts[(k-bucket)%k];counts[bucket]++;}Console.WriteLine(count);}
Divisible Sum Pairs
You are viewing a single comment's thread. Return to all comments →
My O(n) C# solution: