Problem · Array
Perfect Pairs
Learn this problemProblem statement
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 integer array arr of length n, find the number of perfect pairs (arr[i], arr[j]) where 0 ≤ i < j < n.
Here, min(a, b) and max(a, b) are the minimum and maximum of a and b, and |x| is the absolute value of x.
Complete getPerfectPairsCount with the following parameter:
int arr[n]: an array of integers
Returns: long, the number of perfect pairs.
Function
getPerfectPairsCount(arr: int[]) → longExamples
Example 1
arr = [2, 5, 3]return = 2The possible pairs are (2, 5), (2, 3), and (5, 3).
- For
(2, 5),min(3, 7) = 3 > min(2, 5) = 2, so the pair is not perfect. - For
(2, 3),min(1, 5) = 1 ≤ 2andmax(1, 5) = 5 ≥ 3, so the pair is perfect. - For
(5, 3),min(2, 8) = 2 ≤ 3andmax(2, 8) = 8 ≥ 5, so the pair is perfect.
Therefore, the answer is 2.
Constraints
2 ≤ n ≤ 2 * 10^5-10^9 ≤ arr[i] ≤ 10^9
More Snowflake problems
- Closest Target CharacterPHONE SCREEN · Seen Jul 2026
- Horizontal Pod AutoscalerSeen Jul 2026
- Minimum HeightOA · Seen Jul 2026
- Vowel SubstringSeen Jun 2026
- String Formation (Also for AI/ML Software Engineer Intern :)OA · Seen Jun 2026
- Efficient DeploymentsOA · Seen Jun 2026
- Character Frequencies Across Nested String ListsPHONE SCREEN · Seen Jun 2026
- Character Frequencies Across StringsPHONE SCREEN · Seen Jun 2026