go: add «974. Subarray Sums Divisible by K»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
adc9be2dbc
commit
4c4dae49a4
1 changed files with 18 additions and 0 deletions
18
go/subarray-sums-divisible-by-k.go
Normal file
18
go/subarray-sums-divisible-by-k.go
Normal file
|
@ -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
|
||||
}
|
Loading…
Reference in a new issue