Count Index Pairs With a Fixed Value Gap
Arrays & HashingAuto-graded · 24 test casesLast updated
Count index pairs i < j whose values differ by exactly k, using a frequency map and handling the k = 0 case separately to avoid double counting.
Pairs With an Exact Gap
You are given a list of non-negative integers values and a non-negative integer gap.
Count the index pairs (i, j) with i < j for which the two entries differ by exactly gap, that is, abs(values[i] - values[j]) == gap.
Pairs are distinguished by their positions, not by their values. If the same pair of values appears at several different position combinations, each combination counts separately.
Return the total number of such index pairs as an integer.
Special case
When gap = 0, a pair qualifies only if both entries hold the same value, and the two indices must be different. An element is never paired with itself.
Constraints
1 <= len(values) <= 10^50 <= values[i] <= 10^90 <= gap <= 10^9
Examples
Example 1
Input: count_pairs_with_gap([1, 2, 2, 1], 1)
Output: 4
Example 2
Input: count_pairs_with_gap([1, 5, 3, 4, 2], 2)
Output: 3
Hints
Hint 1
Only the values matter, not their positions. Think about how many times each distinct value occurs, and how to store that.
Hint 2
For gap > 0, every valid pair has a smaller value v and a larger value v + gap. Multiply the number of occurrences of v by the number of occurrences of v + gap, and look only in the upward direction so nothing is counted twice.
Hint 3
gap = 0 needs its own formula. A value occurring f times forms f * (f - 1) / 2 pairs, not f * f.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Positions do not affect whether a pair qualifies, so we count how often each value occurs and work from those frequencies.
Case gap > 0. A qualifying pair always has two different values: a smaller one v and a larger one v + gap. Choosing one occurrence of v and one occurrence of v + gap gives exactly one qualifying index pair (the order of the indices is forced by i < j, and the absolute difference does not care which is first). So for each distinct value v, the number of pairs where v is the smaller side is count[v] * count[v + gap]. Summing over all distinct v gives the answer.
Only looking upward (v + gap, never v - gap) is what prevents double counting: each pair is attributed solely to its smaller value.
Case gap = 0. Here both entries have the same value. A value that occurs f times has f * (f - 1) / 2 unordered pairs of distinct positions. The general formula must not be used here, since f * f would pair elements with themselves and count every pair twice.
Why it is correct
Every qualifying index pair maps to exactly one term: for gap > 0, the term of its smaller value; for gap = 0, the term of its shared value. Conversely, every unit counted in a term corresponds to a distinct qualifying index pair. So the sum is exact.
Complexity
- Time: O(n), one pass to build the counts and one pass over the distinct values with O(1) average lookups.
- Space: O(u), where u is the number of distinct values.
The result can reach roughly 5 * 10^9, which Python handles natively. In fixed-width languages a 64-bit accumulator would be needed.
Reference solution
from collections import Counter
from typing import List
def count_pairs_with_gap(values: List[int], gap: int) -> int:
counts = Counter(values)
# gap == 0: pairs of equal values, C(freq, 2) per distinct value
if gap == 0:
return sum(f * (f - 1) // 2 for f in counts.values())
# gap > 0: each pair has a unique smaller value v and larger value v + gap
return sum(f * counts.get(v + gap, 0) for v, f in counts.items())