Skip to content

Commit 6b2c5f9

Browse files
Create Meximum Array.cpp
1 parent 231e5b3 commit 6b2c5f9

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
#include <set>
5+
6+
using namespace std;
7+
8+
void solve()
9+
{
10+
int no_of_elements;
11+
cin >> no_of_elements;
12+
13+
vector <int> A(no_of_elements + 1);
14+
for(int i = 1; i <= no_of_elements; i++)
15+
{
16+
cin >> A[i];
17+
}
18+
19+
vector <int> frequency(no_of_elements + 1, 0);
20+
for(int i = 1; i <= no_of_elements; i++)
21+
{
22+
frequency[A[i]]++;
23+
}
24+
25+
int array_mex = 0;
26+
for(int i = 0; i <= no_of_elements; i++)
27+
{
28+
if(frequency[i] == 0)
29+
{
30+
array_mex = i;
31+
break;
32+
}
33+
}
34+
35+
set <int> all_elements, unseen;
36+
for(int i = 0; i <= array_mex; i++)
37+
{
38+
all_elements.insert(i);
39+
}
40+
unseen = all_elements;
41+
42+
vector <int> answer;
43+
int mex_after_here = array_mex;
44+
for(int i = 1; i <= no_of_elements; i++)
45+
{
46+
frequency[A[i]]--;
47+
48+
if(unseen.find(A[i]) != unseen.end())
49+
{
50+
unseen.erase(A[i]);
51+
}
52+
53+
if(frequency[A[i]] == 0)
54+
{
55+
mex_after_here = min(mex_after_here, A[i]);
56+
}
57+
58+
if(*(unseen.begin()) == array_mex)
59+
{
60+
answer.push_back(array_mex);
61+
array_mex = mex_after_here;
62+
63+
unseen = all_elements;
64+
}
65+
}
66+
67+
cout << answer.size() << "\n";
68+
for(int i = 0; i < answer.size(); i++)
69+
{
70+
cout << answer[i] << " ";
71+
}
72+
cout << "\n";
73+
}
74+
75+
int main()
76+
{
77+
int no_of_test_cases;
78+
cin >> no_of_test_cases;
79+
80+
while(no_of_test_cases--)
81+
solve();
82+
83+
return 0;
84+
}

0 commit comments

Comments
 (0)