---
# System prepended metadata

title: LC 2670. Find the Distinct Difference Array
tags: [easy, leedcode, python]

---

# LC 2670. Find the Distinct Difference Array

### [Problem link](https://leetcode.com/problems/find-the-distinct-difference-array/)

###### tags: `leedcode` `python` `easy`

You are given a  **0-indexed**  array <code>nums</code> of length <code>n</code>.

The  **distinct difference**  array of <code>nums</code> is an array <code>diff</code> of length <code>n</code> such that <code>diff[i]</code> is equal to the number of distinct elements in the suffix <code>nums[i + 1, ..., n - 1]</code>  **subtracted from**  the number of distinct elements in the prefix <code>nums[0, ..., i]</code>.

Return the  **distinct difference**  array of <code>nums</code>.

Note that <code>nums[i, ..., j]</code> denotes the subarray of <code>nums</code> starting at index <code>i</code> and ending at index <code>j</code> inclusive. Particularly, if <code>i > j</code> then <code>nums[i, ..., j]</code> denotes an empty subarray.

**Example 1:** 

```
Input: nums = [1,2,3,4,5]
Output: [-3,-1,1,3,5]
Explanation: For index i = 0, there is 1 element in the prefix and 4 distinct elements in the suffix. Thus, diff[0] = 1 - 4 = -3.
For index i = 1, there are 2 distinct elements in the prefix and 3 distinct elements in the suffix. Thus, diff[1] = 2 - 3 = -1.
For index i = 2, there are 3 distinct elements in the prefix and 2 distinct elements in the suffix. Thus, diff[2] = 3 - 2 = 1.
For index i = 3, there are 4 distinct elements in the prefix and 1 distinct element in the suffix. Thus, diff[3] = 4 - 1 = 3.
For index i = 4, there are 5 distinct elements in the prefix and no elements in the suffix. Thus, diff[4] = 5 - 0 = 5.
```

**Example 2:** 

```
Input: nums = [3,2,3,4,2]
Output: [-2,-1,0,2,3]
Explanation: For index i = 0, there is 1 element in the prefix and 3 distinct elements in the suffix. Thus, diff[0] = 1 - 3 = -2.
For index i = 1, there are 2 distinct elements in the prefix and 3 distinct elements in the suffix. Thus, diff[1] = 2 - 3 = -1.
For index i = 2, there are 2 distinct elements in the prefix and 2 distinct elements in the suffix. Thus, diff[2] = 2 - 2 = 0.
For index i = 3, there are 3 distinct elements in the prefix and 1 distinct element in the suffix. Thus, diff[3] = 3 - 1 = 2.
For index i = 4, there are 3 distinct elements in the prefix and no elements in the suffix. Thus, diff[4] = 3 - 0 = 3.
```

 **Constraints:** 

- <code>1 <= n == nums.length<= 50</code>
- <code>1 <= nums[i] <= 50</code>


## Solution 1
```python=
class Solution:
    def distinctDifferenceArray(self, nums: List[int]) -> List[int]:
        pre_cnt = Counter([])
        suff_cnt = Counter(nums)
        res = []
        
        for num in nums:
            pre_cnt[num] += 1
            suff_cnt[num] -= 1
            if suff_cnt[num] == 0:
                del suff_cnt[num]
            res.append(len(pre_cnt) - len(suff_cnt))
        
        return res
        
```

>### Complexity
>n = nums.length
>|             | Time Complexity | Space Complexity |
>| ----------- | --------------- | ---------------- |
>| Solution 1  | O(n)            | O(n)             |

## Note
x
