Javascript solution - O(n)
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```javascript []
/**
 * @param {number[]} nums
 * @return {number[]}
 */
var distinctDifferenceArray = function (nums) {
    const n = nums.length;
    const result = new Array(n);
    const prefixSet = new Set();
    const suffixSet = new Set();
    const suffixCounts = new Array(n);

    // Calculate suffix set sizes
    for (let i = n - 1; i >= 0; i--) {
        suffixSet.add(nums[i]);
        suffixCounts[i] = suffixSet.size;
    }

    prefixSet.add(nums[0]);
    result[0] = prefixSet.size - (n > 1 ? suffixCounts[1] : 0);

    for (let i = 1; i < n; i++) {
        prefixSet.add(nums[i]);
        result[i] = prefixSet.size - (i + 1 < n ? suffixCounts[i + 1] : 0);
    }

    return result;
};
```
['JavaScript']