FastPrepCount Equal Code Pairs Within Distance K
Problem · Array

Count Equal Code Pairs Within Distance K

Learn this problem
Mediuminfosys logoinfosysNEW GRADONSITE INTERVIEW

Problem 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) → long

Examples

Example 1

code = [1,2,1,1]k = 2return = 2

The 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 = 2

Adjacent 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.

More infosys problems

drafts saved locally
public long countEqualCodePairs(int[] code, int k) {
  // Write your code here.
}
code[1,2,1,1]
k2
expected2
checking account