Number of Perfect Pairs
You are given an integer array nums.
A pair of indices (i, j) is called perfect if the following conditions are satisfied:
i < j- Let
a = 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
2There are 2 perfect pairs:
(1, 2) with values (1, 2) and (2, 3) with values (2, 3).Example 2
Input
nums = [-3,2,-1,4]Output
4There are 4 perfect pairs:
(0, 1), (0, 3), (1, 2), and (1, 3).Constraints
- 2 <= nums.length <= 10^5
- -10^9 <= nums[i] <= 10^9