Count Number of Perfect Pairs
Given a list of integers nums, count the pairs of indices (i, j) where i < j and the corresponding values form a perfect pair.
Two values a = nums.get(i) and b = nums.get(j) form a perfect pair when both of the following conditions are satisfied:
min(|a - b|, |a + b|) <= min(|a|, |b|)
max(|a - b|, |a + b|) >= max(|a|, |b|)
Return the total number of perfect index pairs.
Method Signature
long perfectPairs(List<Integer> nums)
Parameters
nums contains the integers used to form index pairs.
Return Value
Return the number of pairs (i, j) that satisfy both perfect pair conditions.
Constraints
2 <= nums.size() <= 100,000
-1,000,000,000 <= nums.get(i) <= 1,000,000,000
0 <= i < nums.size()
Examples
Example 1
perfectPairs(nums = List.of(-4, -2, 1, 8))
Output: 3
The qualifying index pairs contain the values (-4, -2), (-4, 8), and (-2, 1).
Example 2
perfectPairs(nums = List.of(0, 0, 3, -5, 10))
Output: 3
The qualifying index pairs contain the values (0, 0), (3, -5), and (-5, 10).
Example 3
perfectPairs(nums = List.of(1, 10))
Output: 0
The values do not satisfy the perfect pair conditions.