Given an array A[] of N elements, the task is to create a PrefixMEX array for this given array. PrefixMEX array B[] of an array A[] is created such that MEX of A[0] till A[i] is B[i].
MEX of an array refers to the smallest missing nonnegative integer of the array.
Examples:
Input: A[] = {1, 0, 2, 4, 3}
Output: 0 2 3 3 5
Explanation: In the array A, elements
Till 1st index, elements are [1] and mex till 1st index is 0.
Till 2nd index, elements are [1, 0] and mex till 2nd index is 2.
Till 3rd index, elements are [ 1, 0, 2] and mex till 3rd index is 3.
Till 4th index, elements are [ 1, 0, 2, 4] and mex till 4th index is 3.
Till 5th index, elements are [ 1, 0, 2, 4, 3] and mex till 5th index is 5.
So our final array B would be [0, 2, 3, 3, 5].Input: A[] = [ 1, 2, 0 ]
Output: [ 0, 0, 3 ]
Explanation: In the array A, elements
Till 1st index, elements are [1] and mex till 1st index is 0.
Till 2nd index, elements are [1, 2] and mex till 2nd index is 0.
Till 3rd index, elements are [ 1, 2, 0] and mex till 3rd index is 3.
So our final array B would be [0, 0, 3].
Naive Approach: The simplest way to solve the problem is:
For each element at i^{th} (0 ≤ i < N)index of the array A[], find MEX from 0 to i and store it at B[i].
Follow the steps mentioned below to implement the idea:
 Iterate over the array from i = 0 to N1:
 For every i^{th} index in array A[]:
 Return the resultant array B[] at the end.
Time Complexity: O(N^{2})
Auxiliary Space: O(N)
Efficient Approach: This approach is based on the usage of Set data structure.
A set stores data in sorted order. We can take advantage of that and store all the nonnegative integers till the maximum value of the array. Then traverse through each array element and remove the visited data from set. The smallest remaining element will be the MEX for that index.
Follow the steps below to implement the idea:
 Find the maximum element of the array A[].
 Create a set and store the numbers from 0 to the maximum element in the set.
 Traverse through the array from i = 0 to N1:
 For each element, erase that element from the set.
 Now find the smallest element remaining in the set.
 This is the prefix MEX for the i^{th} element. Store this value in the resultant array.
 Return the resultant array as the required answer.
Below is the implementation of the above approach.
C++

Time Complexity: O(N * log N )
 O(N) for iterating the vector, and
 O(log N) for inserting and deleting the element from the set.
Auxiliary Space: O(N)