Trains the technique from
LeetCode 1512Number of Good 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 wheelbuilder keeps loose spokes in a rack. lengths[i] is the length in millimetres of the spoke in slot i.
Two spokes make a matched pair when they sit in different slots and read the same length. A pair is named by its two slots, so the pair from slots 2 and 5 is the same pair as the one from slots 5 and 2, and a spoke never pairs with itself.
Return the number of matched pairs in the rack. Pairs are counted over all slots at once, so a length held in four slots contributes every pair those four slots make.
Example 1
Length 64 sits in slots 0, 2 and 3, which make the pairs (0,2), (0,3) and (2,3). The spokes at 17 and 29 have no partner.
Example 2
The three spokes all read different lengths, so the rack holds no matched pair.
Example 3
All six slots read 7, and six slots make fifteen distinct pairs.
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 matched_spoke_pairs(lengths: list[int]) -> int:public int matchedSpokePairs(int[] lengths)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.