Number of Good Pairs
Real-world version: Matched Spoke PairsThe statement here is our own: the same inputs, outputs and technique.
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.
Examples
Example 1
- Input
- lengths = [64, 17, 64, 64, 29]
- Output
- 3
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
- Input
- lengths = [46, 82, 91]
- Output
- 0
The three spokes all read different lengths, so the rack holds no matched pair.
Example 3
- Input
- lengths = [7, 7, 7, 7, 7, 7]
- Output
- 15
All six slots read 7, and six slots make fifteen distinct pairs.
Constraints
- 1 <= lengths.length <= 100
- 1 <= lengths[i] <= 100
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def matched_spoke_pairs(lengths: list[int]) -> int:public int matchedSpokePairs(int[] lengths)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 12 test cases: 12 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's