Problem · Array
Count Equal Code Pairs Within Distance K
Learn this problemProblem statement
You are given an integer array code and an integer k.
Return the number of unordered pairs (i, j) such that i < j, code[i] == code[j], and j - i <= k.
Function
countEqualCodePairs(code: int[], k: int) → longExamples
Example 1
code = [1,2,1,1]k = 2return = 2The pairs are indices (0, 2) and (2, 3). Indices (0, 3) have equal values but distance 3, which is greater than k.
Example 2
code = [1,1,1]k = 1return = 2Adjacent equal pairs at distances 1 count. The pair of the first and last values has distance 2.
Constraints
0 <= code.length <= 10^5.0 <= k <= code.length.-10^9 <= code[i] <= 10^9.