mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-10 00:09:06 +01:00
18 lines
310 B
Go
18 lines
310 B
Go
package subarray_sums_divisible_by_k
|
|
|
|
func subarraysDivByK(nums []int, k int) int {
|
|
counters := make([]int, k)
|
|
counters[0] = 1
|
|
|
|
total := 0
|
|
|
|
runningMod := 0
|
|
for _, num := range nums {
|
|
runningMod = (k + runningMod + num%k) % k
|
|
|
|
total += counters[runningMod]
|
|
counters[runningMod]++
|
|
}
|
|
|
|
return total
|
|
}
|