All problems
0453EasyArrayHash TableMathCounting

Matched Spoke Pairs

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1512Number of Good Pairs

This 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.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def matched_spoke_pairs(lengths: list[int]) -> int:
Java
public int matchedSpokePairs(int[] lengths)
September 7
Apply