Problem · Array

Count Perfect Pairs

Learn this problem
MediumAdyen logoAdyenFULLTIMEOA

Problem 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[]) → long

Examples

Example 1

nums = [1,2,3]return = 2

The index pairs containing values (1, 2) and (2, 3) satisfy both inequalities.

Example 2

nums = [0,0,0]return = 3

Every pair has both expressions equal to zero.

Constraints

  • 1 <= nums.length <= 8,000.
  • -10^9 <= nums[i] <= 10^9.

More Adyen problems

drafts saved locally
public long countPerfectPairs(int[] nums) {
    // Count qualifying index pairs.
}
nums[1,2,3]
expected2
checking account