#3649

Number of Perfect Pairs

medium · 33.5% accepted · 95 likes · top 11%

array · math · two pointers · sorting

Description

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.

Solution