🔢 Efficient Distinct Difference Array | Prefix + Hashing | O(N) Optimized 🔥
Intuition

We need to compute the distinct difference array, where each ans[i] is:

count of distinct elements from 0 to i−count of distinct elements from i+1 t
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
- We need to compute the distinct difference array, where each ans[i] is:

    - count of distinct elements from 0 to i−count of distinct elements from i+1 to n−1
- Brute force approach:
    - Use two functions countLDig and countRDig to count distinct elements in left and right subarrays.
    - These functions iterate over elements and use a hashing array (ans[256]) to track unique elements.

- Problem: This approach recomputes distinct counts for each index, leading to inefficiency
# Approach
<!-- Describe your approach to solving the problem. -->
1. **Two Helper Functions:**
    - countLDig`(nums, index, n)`: Counts unique elements from `0` to `index` using an array of size `256` (hashing).
    - countRDig`(nums, index, n)`: Counts unique elements from `index+1` to `n-1` using the same hashing approach.
2. **Iterate through** `nums[]`, calling both helper functions for each index` i`, and store the difference in `ans[i]`.
# Complexity
- **Time complexity:** O(N²)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

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

# Code
```cpp []
class Solution {
public:

    int countLDig(vector<int> nums,int index,int n){
        int count =0;
        vector<int>ans(256,0);
        for(int i = 0 ; i <= index ; i++){
            if(ans[nums[i]]>0){continue;}
            ans[nums[i]]=ans[nums[i]]+1;
            count++;
        }
        return count;
    }
    int countRDig(vector<int> nums,int index,int n){
        int count =0;
        vector<int>ans(256,0);
        for(int i = index+1 ; i < n ; i++){
            if(ans[nums[i]]>0){continue;}
            ans[nums[i]]=ans[nums[i]]+1;
            count++;
        }
        return count;
    }

    vector<int> distinctDifferenceArray(vector<int>& nums) {
       int n = nums.size();
       vector<int>ans(n,0);

       for(int i = 0; i<n;i++){
        int leftCount = countLDig(nums,i,n);
        int rightCount = countRDig(nums,i,n);
        ans[i] = leftCount-rightCount;
       }
       return ans;
    }
};
```
['Array', 'Hash Table', 'Iterator', 'C++']