You are given an integer array nums.
A pair of indices (i, j) is called perfect if the following conditions are satisfied:
i < ja = nums[i], b = nums[j]. Then:
min(|a - b|, |a + b|) <= min(|a|, |b|)max(|a - b|, |a + b|) >= max(|a|, |b|)Return the number of distinct perfect pairs.
Note: The absolute value |x| refers to the non-negative value of x.
Example 1:
Input: nums = [0,1,2,3]
Output: 2
Explanation:
There are 2 perfect pairs:
(i, j) |
(a, b) |
min(|a − b|, |a + b|) |
min(|a|, |b|) |
max(|a − b|, |a + b|) |
max(|a|, |b|) |
|---|---|---|---|---|---|
| (1, 2) | (1, 2) | min(|1 − 2|, |1 + 2|) = 1 |
1 | max(|1 − 2|, |1 + 2|) = 3 |
2 |
| (2, 3) | (2, 3) | min(|2 − 3|, |2 + 3|) = 1 |
2 | max(|2 − 3|, |2 + 3|) = 5 |
3 |
Example 2:
Input: nums = [-3,2,-1,4]
Output: 4
Explanation:
There are 4 perfect pairs:
(i, j) |
(a, b) |
min(|a − b|, |a + b|) |
min(|a|, |b|) |
max(|a − b|, |a + b|) |
max(|a|, |b|) |
|---|---|---|---|---|---|
| (0, 1) | (-3, 2) | min(|-3 - 2|, |-3 + 2|) = 1 |
2 | max(|-3 - 2|, |-3 + 2|) = 5 |
3 |
| (0, 3) | (-3, 4) | min(|-3 - 4|, |-3 + 4|) = 1 |
3 | max(|-3 - 4|, |-3 + 4|) = 7 |
4 |
| (1, 2) | (2, -1) | min(|2 - (-1)|, |2 + (-1)|) = 1 |
1 | max(|2 - (-1)|, |2 + (-1)|) = 3 |
2 |
| (1, 3) | (2, 4) | min(|2 - 4|, |2 + 4|) = 2 |
2 | max(|2 - 4|, |2 + 4|) = 6 |
4 |
Example 3:
Input: nums = [1,10,100,1000]
Output: 0
Explanation:
There are no perfect pairs. Thus, the answer is 0.
Constraints:
2 <= nums.length <= 105-109 <= nums[i] <= 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:
Imagine you have a collection of items and you need to count how many pairs of them match a specific condition of perfection. The brute force approach involves examining every possible pairing one by one to see if they fit the criteria.
Here's how the algorithm would work step-by-step:
def count_perfect_pairs(collection): total_perfect_pairs = 0 collection_length = len(collection) # We iterate through each item to serve as the fixed starting point for our pair combinations for starting_index in range(collection_length): starting_item = collection[starting_index] # We compare the current starting item against all subsequent items to evaluate every unique pairing for comparison_index in range(starting_index + 1, collection_length): comparison_item = collection[comparison_index] # We verify the perfection condition using absolute differences and sums to match problem constraints if abs(starting_item - comparison_item) <= abs(starting_item + comparison_item): total_perfect_pairs += 1 return total_perfect_pairsInstead of comparing every single item with every other item, the optimal approach groups similar items together and sorts them first. This clever organization allows us to count valid pairs quickly using simple arithmetic rather than checking every combination one by one.
Here's how the algorithm would work step-by-step:
def count_perfect_pairs(input_numbers):
sorted_numbers = sorted(input_numbers)
total_valid_pairs = 0
total_elements = len(sorted_numbers)
# Sorting allows us to establish boundaries and use math instead of nested brute-force scans.
outer_pointer = 0
while outer_pointer < total_elements:
inner_pointer = outer_pointer
while inner_pointer < total_elements and sorted_numbers[inner_pointer] <= sorted_numbers[outer_pointer] * 2:
inner_pointer += 1
# Mathematical shortcut calculates valid elements in range instantly instead of iterating.
total_valid_pairs += (inner_pointer - outer_pointer - 1)
outer_pointer += 1
return total_valid_pairs| Case | How to Handle |
|---|---|
| Empty or single-element array | Return zero immediately since no valid pairs of indices can be formed. |
| Array containing negative numbers or zeros | Since elements can be zero or negative, minimum and sum conditions must account for sign behavior to avoid division-by-zero or incorrect comparisons. |
| Arrays with all identical values | Sorting or frequency counting handles identical elements in batch to compute combinations efficiently without $O(N^2)$ overhead. |
| Extremely large inputs up to maximum constraints | An $O(N og N)$ sorting and two-pointer or binary search approach must be used to avoid Time Limit Exceeded errors from naive $O(N^2)$ looping. |
| Integer overflow during sum operations | Use 64-bit integer types for calculating sums and differences when array values approach standard integer limits. |
| Pairs where i equals j | The condition i <= j includes self-pairs, so valid elements that satisfy the inequalities with themselves must be counted. |
| Widely skewed value distributions | Monotonic properties after sorting allow the use of sliding windows to efficiently bound the search space for each element. |
| No valid perfect pairs exist in the array | The algorithm simply completes its traversal and correctly returns a total count of zero. |