-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDistinct Difference
36 lines (33 loc) · 910 Bytes
/
Distinct Difference
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
public:
vector<int> getDistinctDifference(int N, vector<int> &A)
{
// code here
vector<int>distLeft(A.size(),0);
vector<int>distRight(A.size(),0);
set<int>distElementsSet;
vector<int>ans;
distLeft[0]=0;
distRight[A.size()-1]=0;
distElementsSet.insert(A[0]);
//left traversal distinct elements count
for(int i=1;i<A.size();i++)
{
distLeft[i] += distElementsSet.size();
distElementsSet.insert(A[i]);
}
distElementsSet = set<int>(); //re-initialize set
distElementsSet.insert(A[A.size()-1]);
//right traversal distinct elements count
for(int i=A.size()-2;i>=0;i--)
{
distRight[i] += distElementsSet.size();
distElementsSet.insert(A[i]);
}
for(int i=0;i<A.size();i++)
{
ans.push_back(distLeft[i] - distRight[i]);
}
return ans;
}
};