go: add «2176. Count Equal and Divisible Pairs in an Array»

URL:	https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2025-04-17 09:23:21 +02:00
parent 38cea1225d
commit 71a5045a86
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,34 @@
package main
import "iter"
type Pair[T, U any] struct {
First T
Second U
}
type Indices Pair[int, int]
func countPairs(nums []int, k int) int {
Pairs := func(n int) iter.Seq[Indices] {
return func(yield func(Indices) bool) {
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
if !yield(Indices{i, j}) {
return
}
}
}
}
}
count := 0
for indices := range Pairs(len(nums)) {
i, j := indices.First, indices.Second
if (i*j)%k == 0 && nums[i] == nums[j] {
count++
}
}
return count
}