Sum of Distances - Problem

You're given an array of numbers where some values might appear multiple times. For each element in the array, imagine you want to calculate how far away it is from all other elements that have the same value.

More specifically, for each position i in the array, you need to find all other positions j where the values are equal (nums[i] == nums[j] but i โ‰  j), then sum up all the distances |i - j| between these positions.

Goal: Return an array where each element represents the sum of distances from that position to all other positions with the same value.

Example: If nums = [1, 3, 1, 1, 2], then for the first element (value 1 at index 0), it appears again at indices 2 and 4. So the sum of distances is |0-2| + |0-4| = 2 + 4 = 6.

Input & Output

example_1.py โ€” Basic case with repeated values
$ Input: nums = [1, 3, 1, 1, 2]
โ€บ Output: [5, 0, 3, 4, 0]
๐Ÿ’ก Note: For index 0 (value 1): distances to indices 2,3 are |0-2|+|0-3| = 2+3 = 5. For index 1 (value 3): no other 3s, so 0. For index 2 (value 1): distances to indices 0,3 are |2-0|+|2-3| = 2+1 = 3. For index 3 (value 1): distances to indices 0,2 are |3-0|+|3-2| = 3+1 = 4. For index 4 (value 2): no other 2s, so 0.
example_2.py โ€” All unique elements
$ Input: nums = [1, 2, 3, 4]
โ€บ Output: [0, 0, 0, 0]
๐Ÿ’ก Note: Since all elements are unique, there are no other elements with the same value for any position, so all distances are 0.
example_3.py โ€” All same elements
$ Input: nums = [5, 5, 5]
โ€บ Output: [3, 2, 3]
๐Ÿ’ก Note: For index 0: distances to indices 1,2 are |0-1|+|0-2| = 1+2 = 3. For index 1: distances to indices 0,2 are |1-0|+|1-2| = 1+1 = 2. For index 2: distances to indices 0,1 are |2-0|+|2-1| = 2+1 = 3.

Visualization

Tap to expand
Sum of Distances - Mathematical OptimizationInput Array:1031121324Step 1: Group by ValueValue 1: [0, 2, 3]Value 3: [1]Value 2: [4]Step 2: Calculate EfficientlyFor value 1 group [0, 2, 3]:โ€ข Position 0: left=0, right=2+3 = (0ร—0-0) + (5-0) - 2ร—0 = 5โ€ข Position 2: left=2, right=1 = (1ร—2-0) + (3-2) - 1ร—2 = 3โ€ข Position 3: left=3, right=1 = (2ร—3-2) + (0-3) - 0ร—3 = 4Using prefix sums: [0, 0, 2, 5]Final Result:50340Time Complexity: O(n) | Space Complexity: O(n)Mathematical optimization eliminates nested loops!
Understanding the Visualization
1
Group Same Values
Create groups of indices that have the same value, like grouping friends by shirt color
2
Use Prefix Sums
For each group, calculate prefix sums to enable efficient distance calculations
3
Mathematical Formula
For each position, calculate sum of distances using: left_distances + right_distances
4
Linear Time Result
Each element is processed once, achieving O(n) complexity instead of O(nยฒ)
Key Takeaway
๐ŸŽฏ Key Insight: By grouping same values and using prefix sums with mathematical formulas, we can calculate all distances in linear time instead of the naive quadratic approach.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

For each of n elements, we scan through all n elements to find matches

n
2n
โš  Quadratic Growth
Space Complexity
O(1)

Only using constant extra space for variables, excluding output array

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 0 โ‰ค nums[i] โ‰ค 109
  • All array indices are 0-based
Asked in
Google 42 Amazon 31 Meta 28 Microsoft 19
42.8K Views
Medium-High Frequency
~15 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen