nums
of length n
and an integer target
, return the number of pairs (i, j)
where 0 <= i < j < n
and nums[i] + nums[j] < target
.
Example 1:
Input: nums = [-1,1,2,3,1], target = 2 Output: 3 Explanation: There are 3 pairs of indices that satisfy the conditions in the statement: - (0, 1) since 0 < 1 and nums[0] + nums[1] = 0 < target - (0, 2) since 0 < 2 and nums[0] + nums[2] = 1 < target - (0, 4) since 0 < 4 and nums[0] + nums[4] = 0 < target Note that (0, 3) is not counted since nums[0] + nums[3] is not strictly less than the target.
Example 2:
Input: nums = [-6,2,5,-2,-7,-1,3], target = -2 Output: 10 Explanation: There are 10 pairs of indices that satisfy the conditions in the statement: - (0, 1) since 0 < 1 and nums[0] + nums[1] = -4 < target - (0, 3) since 0 < 3 and nums[0] + nums[3] = -8 < target - (0, 4) since 0 < 4 and nums[0] + nums[4] = -13 < target - (0, 5) since 0 < 5 and nums[0] + nums[5] = -7 < target - (0, 6) since 0 < 6 and nums[0] + nums[6] = -3 < target - (1, 4) since 1 < 4 and nums[1] + nums[4] = -5 < target - (3, 4) since 3 < 4 and nums[3] + nums[4] = -9 < target - (3, 5) since 3 < 5 and nums[3] + nums[5] = -3 < target - (4, 5) since 4 < 5 and nums[4] + nums[5] = -8 < target - (4, 6) since 4 < 6 and nums[4] + nums[6] = -4 < target
Constraints:
1 <= nums.length == n <= 50
-50 <= nums[i], target <= 50
When 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 method involves checking every possible combination of two numbers from the list to see if their sum meets our condition. It's like comparing each item with every other item to find matches. We will simply go through all possible pairings.
Here's how the algorithm would work step-by-step:
def count_pairs_brute_force(numbers, target):
pair_count = 0
# Iterate through each number in the list.
for first_index in range(len(numbers)):
# For each number, iterate through the rest of the list.
for second_index in range(first_index + 1, len(numbers)):
# Compare the sum of the pair to the target.
if numbers[first_index] + numbers[second_index] < target:
pair_count += 1
return pair_count
The most efficient way to find these pairs involves first arranging the numbers in order from smallest to largest. Then, we can use a clever approach that avoids checking every single possible pair, saving a lot of time.
Here's how the algorithm would work step-by-step:
def count_pairs_less_than_target(numbers, target):
numbers.sort()
count_of_pairs = 0
left_index = 0
while left_index < len(numbers):
right_index = len(numbers) - 1
# Find the rightmost index such that the sum is less than the target
while left_index < right_index and numbers[left_index] + numbers[right_index] >= target:
right_index -= 1
# If the indices are the same, no pairs are found.
if left_index == right_index:
left_index += 1
continue
# Count pairs: All elements between left and right form valid pairs
count_of_pairs += right_index - left_index
left_index += 1
return count_of_pairs
Case | How to Handle |
---|---|
Null or empty input array | Return 0 if the input array is null or empty because there are no pairs. |
Input array with less than two elements | Return 0 because at least two elements are needed to form a pair. |
Input array with all elements equal | The solution should correctly count pairs meeting the condition even if all elements are the same. |
Input array contains negative numbers | The algorithm must handle negative numbers correctly when calculating the sum of pairs. |
Input array contains zero | The presence of zero should not impact the correctness of the pair counting. |
Target value is very large, potentially causing integer overflow during summation | Using a language with potential for integer overflow should either use a larger numeric type or carefully check for overflow during calculation. |
Target value is very small, potentially leading to many pairs satisfying the condition. | The algorithm's complexity should handle scenarios where many or all pairs meet the condition. |
Input array contains very large or very small numbers which could cause an integer overflow if added | Use a data type with sufficient range or check for potential overflow before adding the numbers. |