Skip to content

Commit 987cc1c

Browse files
Create Appending Mex.txt
1 parent 2cc4349 commit 987cc1c

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
Initially, the mex = 0
2+
3+
At each step,
4+
5+
Case 1 - Element > mex
6+
7+
This is not possible as the element has to be at most the Mex.
8+
9+
If this happens, it's a mistake. Stop.
10+
11+
Case 2 - Element = mex
12+
13+
Then, mex is not mex + 1
14+
15+
Case 3 - Element < mex
16+
17+
This is allowed. Just move on to the next step.
18+
19+
----------------------------------------------------
20+
21+
int main()
22+
{
23+
int no_of_elements;
24+
cin >> no_of_elements;
25+
26+
int mex_so_far = 0;
27+
for(int i = 1; i <= no_of_elements; i++)
28+
{
29+
int element;
30+
cin >> element;
31+
32+
if(element > mex_so_far)
33+
{
34+
cout << i;
35+
return 0;
36+
}
37+
else if(element == mex_so_far)
38+
{
39+
mex_so_far++;
40+
}
41+
}
42+
43+
cout << "-1";
44+
return 0;
45+
}

0 commit comments

Comments
 (0)