Spent way too long on brute force before realizing O(n²) was never going to pass.
First, simplify the 'perfect' condition by analyzing the signs of x and y, showing it reduces to a simple inequality like |x| ≤ 2|y| and |y| ≤ 2|x|. Then, sort the array by absolute value and use two pointers or binary search to count pairs satisfying the condition in O(n log n) time.
Pro tip: Mention that the condition is symmetric and can be checked by sorting absolute values, which avoids O(n^2) and handles 100,000 elements efficiently. Also, clarify edge cases like zeros and negative numbers.
Break down the 'perfect' condition by considering the signs of x and y. Show that it simplifies to |x| ≤ 2|y| and |y| ≤ 2|x|, meaning the absolute values are within a factor of 2.
Since n can be up to 100,000, an O(n^2) solution is too slow. Sort the array by absolute value and use two pointers or binary search to count valid pairs in O(n log n).
After sorting, for each element, find the range of indices where the absolute value is between |x|/2 and 2|x|. Count pairs (i < j) by ensuring each pair is counted once, e.g., by iterating and counting elements after the current index.
Consider zeros (which pair with any number) and negative numbers (absolute values handle them). Ensure the algorithm correctly counts pairs without double-counting.
State that sorting takes O(n log n) and counting takes O(n) with two pointers, so overall O(n log n) time and O(1) extra space (if sorting in place).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.