Trains the technique from
LeetCode 2615Sum of DistancesThis 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 conveyor carries parts in slots numbered 0, 1, 2, and so on. codes[i] is the part code sitting in slot i, and two slots hold the same part exactly when their codes are equal.
For each slot i, its spacing total is the sum of |i - j| over every other slot j that holds the same part code. A slot whose code appears in no other slot has a spacing total of 0.
Return an array holding the spacing total of every slot, in slot order.
Example 1
Part 4 sits in slots 0, 2 and 3. Slot 0 is 2 and 3 away from the other two, giving 5; slot 2 is 2 and 1 away, giving 3; slot 3 is 3 and 1 away, giving 4. Parts 9 and 6 appear once each, so their slots score 0.
Example 2
Every slot holds the same part. Slot 0 is 1, 2 and 3 away from the others, giving 6, and slot 1 is 1, 1 and 2 away, giving 4.
Example 3
All five codes are different, so no slot has a partner and every spacing total is 0.
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 spacing_totals(codes: list[int]) -> list[int]:public long[] spacingTotals(int[] codes)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.