c++ O(n) Prefix, Suffix and Set
\n Describe your approach to solving the problem. \n\n# Complexity\n- Time complexity: O(n)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O
\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    vector<int> distinctDifferenceArray(vector<int>& nums) {\n        unordered_set<int> s1, s2;\n        vector<int> pre, suf(nums.size());\n        int count = 0;\n        // prefix array\n        for(auto i : nums) {\n            if(s1.find(i) == s1.end()) count++;\n            pre.push_back(count);\n            s1.insert(i);\n        }\n        // suffix array\n        count = 0;\n        for(int i = nums.size()-1; i > 0; i--) {\n            if(s2.find(nums[i]) == s2.end()) count++;\n            suf[i - 1] = count;\n            s2.insert(nums[i]);\n        }\n        // ans;\n        for(int i = 0; i < nums.size(); i++) {\n            pre[i] = pre[i] - suf[i];\n        }\n        return pre;\n    }\n};\n```
['Hash Table', 'Suffix Array', 'Prefix Sum', 'C++']