Tap here to switch tabs
Problem
Submissions

Problem: Sum of Distances

Try to solve the Sum of Distances problem.

Statement

You are given an integer array nums. An array arr of the same length is defined such that arr[i] is the sum of ∣i−j∣|i - j| for all indices j satisfying nums[j] == nums[i] and j≠ij \neq i. If no other index shares the same value, then arr[i] is equal to 00.

Return the resulting array arr.

Note: This question is the same as 21212121: Intervals Between Identical Elements.

Constraints:

  • 1≤1 \leq nums.length ≤105\leq 10^5

  • 0≤0 \leq nums[i] ≤109\leq 10^9

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Sum of Distances

Try to solve the Sum of Distances problem.

Statement

You are given an integer array nums. An array arr of the same length is defined such that arr[i] is the sum of ∣i−j∣|i - j| for all indices j satisfying nums[j] == nums[i] and j≠ij \neq i. If no other index shares the same value, then arr[i] is equal to 00.

Return the resulting array arr.

Note: This question is the same as 21212121: Intervals Between Identical Elements.

Constraints:

  • 1≤1 \leq nums.length ≤105\leq 10^5

  • 0≤0 \leq nums[i] ≤109\leq 10^9