Count Valid Sum Pairs
Greedy & IntervalsAuto-graded · 23 test casesLast updated
Tests greedy two-pointer technique on sorted arrays to maximize disjoint pairs meeting a sum condition.
You are given an array values containing n integers and an integer min_sum.
Your task is to form the maximum number of disjoint pairs from the array elements such that each pair (i, j) satisfies values[i] + values[j] >= min_sum. Each array element can be used in at most one pair.
Return the maximum number of valid pairs that can be formed.
Constraints
0 <= n <= 10^5-10^9 <= values[i] <= 10^9-10^9 <= min_sum <= 10^9- Each element may be used at most once
- If the array has an odd length, at least one element will remain unpaired
Examples
Example 1
Input: count_valid_pairs([1, 2, 3, 4, 5], 5)
Output: 2
Example 2
Input: count_valid_pairs([4, 4, 1, 1], 8)
Output: 1
Example 3
Input: count_valid_pairs([-10, -5, 0, 5, 10], 0)
Output: 2
Example 4
Input: count_valid_pairs([], 10)
Output: 0
Hints
Hint 1
Consider sorting the array first. How does ordering help identify which elements can potentially pair together?
Hint 2
Use a two-pointer approach with one pointer at each end of the sorted array. What does it mean if the sum of the smallest and largest remaining elements fails to meet the threshold?
Hint 3
If the smallest remaining element cannot pair with the largest remaining element, can it pair with any other element? This observation drives the greedy strategy.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The optimal strategy uses a greedy two-pointer technique after sorting:
- Sort the array in ascending order
- Initialize two pointers: one at the start (smallest element) and one at the end (largest element)
- Iterate while the pointers haven't crossed:
- If the sum of elements at both pointers meets the threshold, form a pair and move both pointers inward
- Otherwise, the smallest element cannot pair with anything (since even the largest element fails), so advance the left pointer
Why This Works
The correctness relies on a greedy observation: at each step, we consider the smallest remaining element and the largest remaining element.
- If their sum meets the threshold, we pair them. The largest element is the best possible partner for any element, so using it with the smallest valid element is optimal—we don't waste its capacity.
- If their sum is too small, the smallest element cannot pair with any remaining element (since all others are smaller than or equal to the largest). We must discard it.
This greedy choice is optimal because any valid pairing can be rearranged to match our greedy decisions without reducing the pair count (exchange argument).
Complexity
- Time: O(n log n) due to sorting. The two-pointer scan is O(n) since each iteration advances at least one pointer.
- Space: O(n) for the sorted copy. The sorting itself may use O(log n) stack space depending on the algorithm.
Implementation
from typing import List
def count_valid_pairs(values: List[int], min_sum: int) -> int:
sorted_vals = sorted(values)
left, right = 0, len(sorted_vals) - 1
pair_count = 0
while left < right:
if sorted_vals[left] + sorted_vals[right] >= min_sum:
pair_count += 1
left += 1
right -= 1
else:
left += 1
return pair_count