Given a 0-indexed integer array nums of size n and two integers lower and upper, return the number of fair pairs.
A pair (i, j) is fair if:
0 <= i < j < n, andlower <= nums[i] + nums[j] <= upperExample 1:
Input: nums = [0,1,7,4,4,5], lower = 3, upper = 6 Output: 6 Explanation: There are 6 fair pairs: (0,3), (0,4), (0,5), (1,3), (1,4), and (1,5).
Example 2:
Input: nums = [1,7,9,2,5], lower = 11, upper = 11 Output: 1 Explanation: There is a single fair pair: (2,3).
Constraints:
1 <= nums.length <= 105nums.length == n-109 <= nums[i] <= 109-109 <= lower <= upper <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding fair pairs is like checking every possible combination of numbers to see if they meet the fairness criteria. We will consider each pair of numbers in the given collection one by one. We will then count how many of these pairs are deemed 'fair'.
Here's how the algorithm would work step-by-step:
def count_fair_pairs_brute_force(numbers, lower_bound, upper_bound):
fair_pair_count = 0
# Iterate through each number in the list.
for first_index in range(len(numbers)):
# For each number, iterate through all other numbers (including itself)
for second_index in range(len(numbers)):
# Calculate the sum of the current pair.
current_sum = numbers[first_index] + numbers[second_index]
# Check if the sum falls within the specified range.
# If it does, increment the fair pair count.
if lower_bound <= current_sum <= upper_bound:
fair_pair_count += 1
return fair_pair_countTo efficiently count fair pairs, we will sort the numbers first. Then, we'll use a clever approach to quickly find how many pairs meet the fairness criteria without checking every single combination.
Here's how the algorithm would work step-by-step:
def count_fair_pairs(numbers, lower_limit, upper_limit):
numbers.sort()
number_of_fair_pairs = 0
for first_index in range(len(numbers)):
#Avoid double counting by checking pairs ahead
for second_index in range(first_index + 1, len(numbers)):
sum_of_pair = numbers[first_index] + numbers[second_index]
# Check if the pair meets fairness criteria
if lower_limit <= sum_of_pair <= upper_limit:
number_of_fair_pairs += 1
return number_of_fair_pairs| Case | How to Handle |
|---|---|
| Empty input array | Return 0 if the input array is empty because no pairs can be formed. |
| Array with only one element | Return 0 as a pair requires at least two elements, and the problem definition specifies i < j. |
| Array with two elements where the sum falls outside the range [lower, upper] | Return 0 because the single possible pair is not fair. |
| Array with two elements where the sum falls inside the range [lower, upper] | Return 1 because the single possible pair is fair. |
| Large array with many fair pairs | Ensure the chosen algorithm (e.g., sorting and two-pointers) scales efficiently to avoid exceeding time limits. |
| Array with all identical values | The algorithm should correctly count the number of valid pairs (i, j) where i < j when all values are the same. |
| Negative numbers, zeros, and positive numbers in the array | The algorithm must correctly handle negative, zero, and positive numbers without causing unexpected behavior such as integer overflows or incorrect comparisons. |
| lower > upper | Return 0 since no sum can be between lower and upper and therefore there are no fair pairs. |