```python
import collections

def count_pairs(arr, n, k):
    """
    Counts all the distinct pairs having a difference of k in an array.
    
    Args:
        arr: A list of integers.
        n: The size of the array.
        k: The target difference.
        
    Returns:
        An integer representing the count of distinct pairs.
    """
    if k < 0:
        k = -k

    count = 0
    if k == 0:
        # For k=0, we count the number of distinct elements that appear more than once.
        freq = collections.Counter(arr)
        for num_count in freq.values():
            if num_count > 1:
                count += 1
    else:
        # For k > 0, we find pairs (x, y) where y = x + k.
        # Using a set of unique elements avoids duplicate pairs and allows for efficient lookups.
        unique_elements = set(arr)
        for x in unique_elements:
            if x + k in unique_elements:
                count += 1
    
    return count
```