Get Perfect Pairs Count
A pair of Integers (x, y) is perfect if both of the following conditions are met:
Given an array arr of length n, find the number of perfect pairs
(arr[i], arr[j]) where 0 ≤ i < j < n.
Here min(a, b) is the minimum of a and b,
max(a, b) is the maximum of a and b, and
|x| is the absolute value of x.
Complete the function getPerfectPairsCount in the editor.
getPerfectPairsCount has the following parameter:
int arr[n]: an array of integers
Returns
int: the number of perfect pairs
‧₊˚ ☁️⋅♡𓂃 ࣪ ִֶָ☾. Credit to ✨ BananaInc and spike!! ✨
1Example 1
In this example, n = 3. The possible pairs are (2, 5), (2, -3) and (5, -3).
- (2, 5) is not perfect
- min(|2 - 5|, |2 + 5|) = 3, min(|2|, |5|) = 2
- It fails the first test: 3 > 2
- min(|2 - 5|, |2 + 5|) = 3, min(|2|, |5|) = 2
- (2, -3) is perfect
- min(|5|, |-1|) = 1, min(|2|, |-3|) = 2
- It passes the first test: 1 ≤ 2
- max(|5|, |-1|) = 5, max(|2|, |-3|) = 3
- It passes the second test: 5 ≥ 3
- min(|5|, |-1|) = 1, min(|2|, |-3|) = 2
- (5, -3) is perfect
- min(|8|, |2|) = 2, min(|5|, |-3|) = 3
- It passes the first test: 2 ≤ 3
- max(|8|, |2|) = 8, max(|5|, |-3|) = 5
- It passes the second test: 8 ≥ 5
- min(|8|, |2|) = 2, min(|5|, |-3|) = 3
Therefore, there are 2 perfect pairs.
Constraints
Limits and guarantees your solution can rely on.
2 ≤ n ≤ 2*105-109 ≤ arr[i] ≤ 109