go: add «1497. Check If Array Pairs Are Divisible by k»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
3033003e6c
commit
133298c92a
1 changed files with 19 additions and 0 deletions
19
go/check-if-array-pairs-are-divisible-by-k.go
Normal file
19
go/check-if-array-pairs-are-divisible-by-k.go
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
func canArrange(arr []int, k int) bool {
|
||||||
|
counters := make([]int, k)
|
||||||
|
for _, x := range arr {
|
||||||
|
counters[(x%k+k)%k]++
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < k && counters[i] != -1; i++ {
|
||||||
|
j := (k - i) % k
|
||||||
|
if counters[i] != counters[j] || (i == j && counters[i]%2 == 1) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
counters[i], counters[j] = -1, -1
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
Loading…
Reference in a new issue