Program to count maximum number of distinct pairs whose differences are larger than target in Python

Suppose we have a list of numbers called nums and another value target. We have to find the maximum number of pairs where for each pair i < j, i and j are not in any other pair, and |nums[i] - nums[j]| >= target.

So, if the input is like nums = [2, 4, 6, 10, 11], target = 5, then the output will be 2, as we can get pairs: (2, 10) and (4, 11).

Algorithm

To solve this problem, we will follow these steps ?

  • Sort the array to enable efficient pairing
  • Use two pointers: one for smaller elements, one for larger elements
  • Start the second pointer from the middle of the array
  • For each smaller element, find the first larger element that satisfies the condition
  • Count valid pairs and move both pointers forward

Implementation

def count_distinct_pairs(nums, target):
    n = len(nums)
    nums.sort()
    
    ans = 0
    j = n // 2  # Start from middle
    
    # Try to pair each element from first half with elements from second half
    for i in range(n // 2):
        # Find the first element that satisfies the condition
        while j < n and nums[j] - nums[i] < target:
            j += 1
        
        # If we found a valid pair
        if j < n:
            ans += 1
            j += 1  # Move to next element for next pairing
    
    return ans

# Test the function
nums = [2, 4, 6, 10, 11]
target = 5
result = count_distinct_pairs(nums, target)
print(f"Maximum distinct pairs: {result}")
Maximum distinct pairs: 2

How It Works

The algorithm works by sorting the array first, then using a greedy approach ?

  1. Sorting: [2, 4, 6, 10, 11] remains the same as it's already sorted
  2. Two pointers: i starts from 0, j starts from middle (index 2)
  3. First iteration: i=0 (value=2), find j where nums[j] - 2 >= 5, so j=4 (value=11), but we need j=3 (value=10). Pair (2,10) formed
  4. Second iteration: i=1 (value=4), j=4 (value=11), 11-4=7 >= 5, so pair (4,11) formed

Alternative Class-based Implementation

class Solution:
    def solve(self, nums, target):
        n = len(nums)
        nums.sort()
        ans = 0
        j = n >> 1  # Bit shift equivalent to n // 2
        
        for i in range(n >> 1):
            while j < n and nums[j] - nums[i] < target:
                j += 1
            if j < n:
                ans += 1
                j += 1
        return ans

# Test with class
solution = Solution()
nums = [2, 4, 6, 10, 11]
target = 5
print(solution.solve(nums, target))
2

Time and Space Complexity

  • Time Complexity: O(n log n) due to sorting, where n is the length of the array
  • Space Complexity: O(1) as we only use constant extra space

Conclusion

This greedy approach efficiently finds the maximum number of distinct pairs by sorting the array and using two pointers. The key insight is starting the second pointer from the middle to ensure we can form the maximum possible pairs.

Updated on: 2026-03-25T11:30:57+05:30

378 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements