labels. For every position i, compute the sum of distances |i − j| to every other position j holding the same value. If there is no such position, the sum is 0.
Return an array of these sums, the same length as labels.
Comparing every pair is O(n²); do better.labels=[7, 2, 7, 2, 7] → [6, 2, 4, 2, 6]
The 7s sit at positions 0, 2, 4: position 0 gets 2 + 4 = 6, position 2 gets 2 + 2 = 4, position 4 gets 4 + 2 = 6. The 2s at positions 1 and 3 are 2 apart. The answer is [6, 2, 4, 2, 6].
labels=[9, 8] → [0, 0]
No element has a twin, so both get 0.
labels=[0, 0, 0] → [3, 2, 3]
labels=[7, 2, 7, 2, 7]
[6, 2, 4, 2, 6]
Run checks the first 3 tests in your browser. Submit checks all 108 tests on the server, hidden ones included.