go: add «1497. Check If Array Pairs Are Divisible by k»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-10-01 22:48:13 +02:00
parent 3033003e6c
commit 133298c92a
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View 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
}