Intervals Between Identical Elements - Problem

You are given a 0-indexed array of n integers arr.

The interval between two elements in arr is defined as the absolute difference between their indices. More formally, the interval between arr[i] and arr[j] is |i - j|.

Return an array intervals of length n where intervals[i] is the sum of intervals between arr[i] and each element in arr with the same value as arr[i].

Note: |x| is the absolute value of x.

Input & Output

Example 1 — Basic Array
$ Input: arr = [2,1,3,1,2,3,3]
Output: [4,2,7,2,4,4,5]
💡 Note: For arr[0]=2: same values at indices 0,4 → distances |0-0|+|0-4| = 0+4 = 4. For arr[1]=1: same values at indices 1,3 → distances |1-1|+|1-3| = 0+2 = 2. For arr[2]=3: same values at indices 2,5,6 → distances |2-2|+|2-5|+|2-6| = 0+3+4 = 7.
Example 2 — All Same Values
$ Input: arr = [10,10,10]
Output: [3,2,3]
💡 Note: For arr[0]=10: distances to indices 0,1,2 → |0-0|+|0-1|+|0-2| = 0+1+2 = 3. For arr[1]=10: distances to indices 0,1,2 → |1-0|+|1-1|+|1-2| = 1+0+1 = 2. For arr[2]=10: distances to indices 0,1,2 → |2-0|+|2-1|+|2-2| = 2+1+0 = 3.
Example 3 — All Different Values
$ Input: arr = [1,2,3,4]
Output: [0,0,0,0]
💡 Note: Each element is unique, so each only matches with itself at distance 0. All intervals are 0.

Constraints

  • n == arr.length
  • 1 ≤ n ≤ 105
  • 1 ≤ arr[i] ≤ 105

Visualization

Tap to expand
Intervals Between Identical Elements INPUT arr = [2,1,3,1,2,3,3] 0 1 2 3 4 5 6 2 1 3 1 2 3 3 Group by Value: Value 1: indices [1, 3] Value 2: indices [0, 4] Value 3: indices [2, 5, 6] For arr[2]=3 at index 2: |2-2|+|2-5|+|2-6| = 0+3+4 = 7 Sum of distances to same values ALGORITHM STEPS 1 Group Indices Map each value to its indices hashMap = { 2: [0,4], 1: [1,3], 3: [2,5,6] } 2 Prefix Sums Compute prefix sums for each group 3 Calculate Intervals For position k in group of size n: left = k*idx[k] - prefix[k] right = (prefix[n]-prefix[k+1]) - (n-k-1)*idx[k] 4 Store Results result[idx[k]] = left + right Time: O(n) | Space: O(n) Single pass through each group FINAL RESULT Output Array: 0 1 2 3 4 5 6 4 2 7 2 4 4 5 Verification: Value 1 (indices 1,3): idx 1: |1-3| = 2 [OK] idx 3: |3-1| = 2 [OK] Value 2 (indices 0,4): idx 0: |0-4| = 4 [OK] idx 4: |4-0| = 4 [OK] Value 3 (indices 2,5,6): idx 2: |2-5|+|2-6| = 3+4 = 7 [OK] idx 5: |5-2|+|5-6| = 3+1 = 4 [OK] idx 6: |6-2|+|6-5| = 4+1 = 5 [OK] All Values Match! Key Insight: Use prefix sums to compute interval sums in O(1) per element. For each position k in a group, the sum of distances to elements on the left is: k*current_index - sum_of_left_indices. Similarly for the right side. This avoids O(n^2) pairwise comparisons by leveraging mathematical properties. TutorialsPoint - Intervals Between Identical Elements | Optimal Solution
Asked in
Facebook 12 Amazon 8 Google 6
28.4K Views
Medium Frequency
~25 min Avg. Time
892 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