Leetcode 1016 Solution

This article provides solution to leetcode question 1016 (subarray-sums-divisible-by-k)

https://leetcode.com/problems/subarray-sums-divisible-by-k

Solution

class Solution:
    def subarraysDivByK(self, A: List[int], K: int) -> int:
        m = [0] * K
        m[0] = 1

        s = 0
        for a in A:
            s += a
            m[s % K] += 1

        return int(sum([v * (v - 1) / 2 for v in m]))