Sum Index Distances to Every Equal-Valued Occurrence
Company: Bloomberg
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
### Problem
For each index i in nums, add |i-j| over all other indices j holding the same value. Return these sums in original index order as `vector<long long>`. An entry with no equal-valued companion contributes zero.
```cpp
class Solution {
public:
vector<long long> distance(vector<int>& nums);
};
```
### Examples
**Example 1:**
```text
Input: nums = [4,2,4,2,4]
Output: [6,2,4,2,6]
Explanation: The positions of 4 are 0,2,4, whose distance sums are 6,4,6. The two positions of 2 are distance 2 apart.
```
**Example 2:**
```text
Input: nums = [8,9]
Output: [0,0]
Explanation: Neither value occurs a second time.
```
### Constraints
- `1 <= nums.length <= 100000`.
- `0 <= nums[i] <= 10^9`.
### Clarifying Questions
- Are distances between values or indices? Indices.
- Which other positions contribute? Only positions with an equal value.
- Is the current position counted? Excluding it gives the same sum because its distance is zero.
- What if no other equal value exists? Return zero at that index.
```hint Check the boundary
Adjacent positions with different values do not contribute to each other.
```
Overview: For each array position, sum absolute index distances to all other positions containing the same value and return the sums in original order.