1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-09 15:59:06 +01:00
LeetCode/go/subarray-sums-divisible-by-k.go
Matej Focko d2ef757754
go: allow testing
Signed-off-by: Matej Focko <me@mfocko.xyz>
2024-06-16 11:44:08 +02:00

18 lines
286 B
Go

package main
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
}