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
Inputnums = [0,1,2,3]
Output2
There are 2 perfect pairs: (1, 2) with values (1, 2) and (2, 3) with values (2, 3).
Example 2
Inputnums = [-3,2,-1,4]
Output4
There 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

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate