Company: Apple
Difficulty: medium
Problem Description A pair of integers (x, y) is perfect if both of the following conditions are met: - min(|x - y|, |x + y|) ≤ min(|x|, |y|) - max(|x - y|, |x + y|) ≥ max(|x|, |y|) Given an array arr of length n, count the number of perfect pairs (arr[i], arr[j]) where 0 ≤ i < j < n. Here min(a, b) is the smaller of a and b, max(a, b) is the larger of a and b, and |x| denotes the absolute value of x. Examples Example 1 Input: arr = [2, 5, -3] Here, n = 3, giving the candidate pairs (2, 5), (2, -3), and (5, -3). Checking whether (2, 5) qualifies as a perfect pair: - For x = 2, y = 5: - Condition 1: min(|x - y|, |x + y|) ≤ min(|x|, |y|) - Left side: min(|2 - 5|, |2 + 5|) = min(|-3|, |7|) = min(3, 7) = 3 - Right side: min(|2|, |5|) = min(2, 5) = 2 - Since 3 > 2, the first condition (3 ≤ 2) does not hold. - So (2, 5) is not a perfect pair.