Given a 0-indexed array of integers, you need to calculate the sum of intervals between each element and all other elements with the same value.
An interval between two elements is the absolute difference between their indices. For example, if we have elements at positions i and j, the interval is |i - j|.
Your task: For each element in the array, find the sum of intervals between that element and all other elements with the same value, then return this information as an array.
Example: If arr = [2,1,3,1,2,3,3], then for the element 2 at index 0, we find all other 2s (at index 4) and calculate the interval: |0-4| = 4. So intervals[0] = 4.
Input & Output
Visualization
Time & Space Complexity
Single pass through the array, each operation inside the loop is O(1)
Hash map stores at most n different values with their metadata
Constraints
- n == arr.length
- 1 โค n โค 105
- 1 โค arr[i] โค 105