So, for instance, given [1, 2, 3, 4, 5, 6], we have five couples of items with sum divisible by 3:
(1, 2), (1, 5), (2, 4), (3, 6), (4, 5)This is a HackerRank algorithm problem, implementation section.
Naive solution
Test all the couples, avoiding duplicates. If we check (a1, a2), we don't have to check (a2, a1).
The code I have written for this solution should be of immediate comprehension, even if you are not that much into Python:
result = 0 for i in range(len(values) - 1): # 1 for j in range(i+1, len(values)): # 2 if (values[i] + values[j]) % k == 0: # 3 result += 11. Loops on all the indices in the list but the last one.
2. Loops from the next index to the current "i" to the last one.
3. Check the current couple, and increase the result if compliant.
Even if this is what HackerRank was expecting from us (the problem is marked as easy), we can do better than this, considering its disappointing O(N^2) time complexity.
Linear solution
The problem could be restated as counting the couples that, added up, equal to zero modulo k. Following this insight, let's partition the items accordingly to their own modulo.
remainders = [0] * k for i in range(len(values)): remainders[values[i] % k] += 1Each element in the "remainders" list represents the number of items in the original list having as modulo the index of the element.
For the example shown above we'll get this remainders:
[2, 2, 2]Now, if we add an element having remainder x to element with remainder k - x, we'll get a number equal zero modulo k. We want all the possible combinations of the x elements with the k - x ones, so we apply the Cartesian product to the two sets, that has a size that is their product.
There are a couple of exceptions to this rule. The elements having modulo zero have to be added among themselves, and the same happens to the element having as modulo half k, if k is even. The number of combinations of a set of N elements could be expressed as N * (N-1) / 2.
Putting all together we have this code:
result = remainders[0] * (remainders[0] - 1) // 2 # 1 pivot = k // 2 # 2 if k%2: pivot += 1 # 3 else: result += remainders[k//2] * (remainders[k//2] - 1) // 2 # 4 for i in range(1, pivot): # 5 result += remainders[i] * remainders[k-i]1. Initialize "result" using the above described formula for the modulo zero items.
2. Let's calculate the central element in the list, where we have stop looping to sum up.
3. If k is odd, we won't have a lonely central element, and the pivot should be moved a step to the right.
4. When k is even, the elements having half-k modulo are managed as the zero modulo ones.
5. Normal cases.
After the for-loop, result contains the answer to the original question.
I pushed a python script with both solutions, naive and remainder based, to GitHub, along with a few tests.