/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static int calMEX(int[] copy){
        HashSet<Integer> st = new HashSet<>();
        for(int i : copy) st.add(i);

        for(int i = 0; i < copy.length; i++){
            if(!st.contains(i)) return i;
        }
        return copy.length;
    }

    public static int[] maximumMEX(int[] nums) {
        int[] copy = nums.clone();
        int n = nums.length;
        ArrayList<Integer> ls = new ArrayList<>();
        int i = 0;

        while(i < n){
            int totMEX = calMEX(copy);
            Set<Integer> seen = new HashSet<>();
            int currMEX = 0;
            for(int j = i; j < n; j++){
                seen.add(nums[j]);
                while(seen.contains(currMEX)){
                    currMEX++;
                }
                if(currMEX == totMEX){
                    ls.add(totMEX);
                    copy = Arrays.copyOfRange(nums, j + 1, n);
                    i = j + 1;
                    break;
                }
            }
        }
        int[] ans = new int[ls.size()];
        for(int k = 0; k < ls.size(); k++){
            ans[k] = ls.get(k);
        }
        return ans;
    }
    
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		int[] nums = {0,1,0};
		 int[] temp = maximumMEX(nums);
		 for(int i : temp){
		 	System.out.println(i);
		 }
	}
}