Find Mex

3.6

15 votes
, Algorithms, Brute Force, Implementation, Linear Search, Iterators
Problem

You are given an integer array of length N. You have to find MEX of ith element for all 1iN.

MEX of the ith element is the minimum element greater than or equal to 0 which is not present in array till the ith index.

 

Input Format:

First line contains an integer N denoting the size of array.

Next line contains N integers denoting the elements of the array.

 

Output Format:

Print N integers. ith element should be the MEX of the array prefix till i

 

Constraints:

1N21050arr[i]2105

Sample Input
5
1 0 5 5 3
Sample Output
0 2 2 2 2
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For first test case mex of first index is 0. As it is note present in array

mex of second index is 2 as 0 and 1 is present in array.

mex of 3rd, 4th and 5th index is 2.

Editor Image

?