Count the Number of Fair Pairs - Problem

๐ŸŽฏ Count the Number of Fair Pairs

Imagine you're organizing a team-building activity where participants need to be paired up based on their combined skill levels. You have an array of skill ratings and need to find pairs that fall within an acceptable range!

Given a 0-indexed integer array nums of size n and two integers lower and upper, your task is to count how many "fair pairs" exist in the array.

A pair (i, j) is considered fair if:

  • 0 <= i < j < n (different indices, order matters)
  • lower <= nums[i] + nums[j] <= upper (sum within acceptable range)

Example: If nums = [0,1,7,4,4,5], lower = 3, upper = 6
Valid pairs: (1,3), (1,4), (1,5), (2,3), (3,4), (4,5)
Answer: 6 fair pairs

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [0,1,7,4,4,5], lower = 3, upper = 6
โ€บ Output: 6
๐Ÿ’ก Note: Valid pairs: (0,3): 0+4=4 โœ“, (0,4): 0+4=4 โœ“, (0,5): 0+5=5 โœ“, (1,3): 1+4=5 โœ“, (1,4): 1+4=5 โœ“, (1,5): 1+5=6 โœ“. Total: 6 pairs
example_2.py โ€” Edge Case
$ Input: nums = [1,7,9,2,5], lower = 11, upper = 11
โ€บ Output: 1
๐Ÿ’ก Note: Only one pair sums to exactly 11: (2,4) where nums[2]=9 and nums[4]=5, so 9+5=11. All other pairs sum to different values.
example_3.py โ€” No Valid Pairs
$ Input: nums = [1,2,3], lower = 10, upper = 15
โ€บ Output: 0
๐Ÿ’ก Note: Maximum possible sum is 2+3=5, which is less than lower=10. No pairs can satisfy the condition lower โ‰ค sum โ‰ค upper.

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • -109 โ‰ค nums[i], lower, upper โ‰ค 109
  • lower โ‰ค upper (guaranteed valid range)

Visualization

Tap to expand
๐ŸŽฏ Fair Pair Matching StrategyOriginal Problem: nums=[0,1,7,4,4,5], range=[3,6]Step 1: Sort Array014457Step 2: For person '1', find valid partnersNeed: 3 โ‰ค 1 + partner โ‰ค 6So: 2 โ‰ค partner โ‰ค 5Valid RangeStep 3: Binary Search Results๐Ÿ” Left bound: first element โ‰ฅ 2 โ†’ index 2 (value 4)๐Ÿ” Right bound: last element โ‰ค 5 โ†’ index 4 (value 5)๐Ÿ“Š Count: 4 - 2 + 1 = 3 valid partnersKey Insight:Instead of checking 1 vs 4, 1 vs 4, 1 vs 5 individually...Binary search finds the range [2,4] and counts 3 partners in O(log n)!๐ŸŽฏ Total Time: O(n log n) vs O(nยฒ) brute forceSmart counting beats checking every combination!
Understanding the Visualization
1
Sort for Efficiency
Line up all elements in order to enable binary search - like organizing a speed dating event by age
2
Calculate Valid Range
For each element, determine what values its partner needs to have for their sum to be 'fair'
3
Binary Search Bounds
Quickly find the leftmost and rightmost valid partners using binary search
4
Count the Range
Count all valid partners in one operation instead of checking each individually
Key Takeaway
๐ŸŽฏ Key Insight: Sorting enables us to use binary search for range counting, transforming an O(nยฒ) pair-checking problem into an O(n log n) range-finding solution!
Asked in
Google 42 Amazon 35 Meta 28 Microsoft 31 Apple 22
42.7K Views
Medium-High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen