------
## 最终结论
```python
def count_pairs(arr, n, k):
    pairs_count = 0
    seen = set()
    
    for number in arr:
        if number + k in seen:
            pairs_count += 1
        if number - k in seen:
            pairs_count += 1
        seen.add(number)
    
    return pairs_count

# Test cases
assert count_pairs([1, 5, 3, 4, 2], 5, 3) == 2
assert count_pairs([8, 12, 16, 4, 0, 20], 6, 4) == 5
assert count_pairs([2, 4, 1, 3, 4], 5, 2) == 3
```