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:
parent
38cea1225d
commit
71a5045a86
1 changed files with 34 additions and 0 deletions
34
go/count-equal-and-divisible-pairs-in-an-array.go
Normal file
34
go/count-equal-and-divisible-pairs-in-an-array.go
Normal 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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue