• + 0 comments

    O(n+k)

    def divisibleSumPairs(n, k, ar):
        from collections import Counter
        modulo_count = Counter(num % k for num in ar)
        count = 0
        for i in range(k):
            j=-i % k
            
            num1 = modulo_count.get(i, 0)
            num2 = modulo_count.get(j, 0)
            
            if i>j or num1==0 or num2==0:
                continue
    
            if i==j:
                count+= num1*(num1-1)//2
            else:
                count+=num1*num2
        return count