Trains the technique from
LeetCode 3649Number of Perfect PairsThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A bench of dials has been trimmed, trims[i] being the trim on the i-th dial. A trim may be negative.
Two trims a and b agree when both of these hold:
|a - b| and |a + b| is no more than the smaller of |a| and |b|;|a - b| and |a + b| is no less than the larger of |a| and |b|.Return the number of pairs of positions i < j whose trims agree.
Example 1
Magnitudes 2, 4, 7 and 9. The pairs that agree are 2 with 4, 4 with 7, and 7 with 9, since each larger magnitude is within double the smaller. 2 with 7, 2 with 9 and 4 with 9 all miss.
Example 2
Signs fall away, leaving magnitudes 8, 4, 4 and 8. Since 8 is exactly double 4, and equal magnitudes always agree, all six pairs on the bench agree.
Example 3
The smaller of the two differences is 3 and the smaller magnitude is 0, so the first test fails and the only pair on the bench does not agree.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def perfect_pairs(trims: list[int]) -> int:public long perfectPairs(int[] trims)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.