diff --git a/go/subarray-sums-divisible-by-k.go b/go/subarray-sums-divisible-by-k.go new file mode 100644 index 0000000..ce0e9be --- /dev/null +++ b/go/subarray-sums-divisible-by-k.go @@ -0,0 +1,18 @@ +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 +}