-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution2006.go
More file actions
35 lines (30 loc) · 868 Bytes
/
Copy pathsolution2006.go
File metadata and controls
35 lines (30 loc) · 868 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package solution2006
// ============================================================================
// 2006. Count Number of Pairs With Absolute Difference K
// URL: https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/
// ============================================================================
/*
$ go test -bench=. -benchmem
goos: linux
goarch: amd64
pkg: GoLeetCode/solutions/2006---Count-Number-of-Pairs-with-Absolute-Difference-K
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_count-24 487892126 2.387 ns/op 0 B/op 0 allocs/op
PASS
*/
func countKDifference(nums []int, k int) int {
ans := 0
val := 0
for i := 0; i < len(nums); i++ {
for j := i; j < len(nums); j++ {
val = nums[i] - nums[j]
if val < 0 {
val = -val
}
if val == k {
ans++
}
}
}
return ans
}