Problem · Array
Count Perfect Pairs
Learn this problemProblem statement
You are given an integer array nums. Count index pairs (i, j) with i < j for which, setting x = nums[i] and y = nums[j], both conditions hold:
min(|x - y|, |x + y|) <= min(|x|, |y|).max(|x - y|, |x + y|) >= max(|x|, |y|).
Return the number of qualifying index pairs.
Function
countPerfectPairs(nums: int[]) → longExamples
Example 1
nums = [1,2,3]return = 2The index pairs containing values (1, 2) and (2, 3) satisfy both inequalities.
Example 2
nums = [0,0,0]return = 3Every pair has both expressions equal to zero.
Constraints
1 <= nums.length <= 8,000.-10^9 <= nums[i] <= 10^9.