Ehab and Prefix MEXs (Codeforces 1364C)
Tags: Greedy, brute force

Given an array $$$a$$$ of length $$$n$$$, find another array, $$$b$$$, of length $$$n$$$ such that:  for each $$$i$$$ $$$(1 \le i \le n)$$$ $$$MEX(\{b_1$$$, $$$b_2$$$, $$$\ldots$$$, $$$b_i\})=a_i$$$. The $$$MEX$$$ of a set of integers is the smallest non-negative integer that doesn't belong to this set.If such array doesn't exist, determine this.

Input: The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the array $$$a$$$.The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, $$$\ldots$$$, $$$a_n$$$ ($$$0 \le a_i \le i$$$) — the elements of the array $$$a$$$. It's guaranteed that $$$a_i \le a_{i+1}$$$ for $$$1\le i < n$$$. 

Output: If there's no such array, print a single line containing $$$-1$$$.Otherwise, print a single line containing $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, $$$\ldots$$$, $$$b_n$$$ ($$$0 \le b_i \le 10^6$$$)If there are multiple answers, print any.

Note: In the second test case, other answers like $$$[1,1,1,0]$$$, for example, are valid.

Sample Inputs:
3
1 2 3

4
0 0 0 2

3
1 1 3

Sample Outputs:
0 1 2

1 3 4 0

0 2 1

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
n = int(input())
a = list(map(int, input().split()))
b = [-1] * n
used = [False] * n
if a[0] == 1:
    b[0] = 0
    used[0] = True
for i in range(1, n):
    if a[i - 1] < a[i]:
        b[i] = a[i - 1]
        used[a[i - 1]] = True
req = [i for i in range(a[-1]) if not used[i]][::-1]
for i in range(n):
    if b[i] == -1:
        if req:
            b[i] = req.pop()
        else:
            b[i] = n + 1
print(*b)

Solution (Python):
def solve():
    n = int(input())
    a = list(map(int, input().split()))
    a_set = set(a)
    not_in_a = []
    for i in range(2 * n + 5):
        if i not in a_set:
            not_in_a.append(i)
    cur_idx = 1
    b = [not_in_a[0]]
    for i in range(1, n):
        if a[i] != a[i - 1]:
            b.append(a[i - 1])
        else:
            b.append(not_in_a[cur_idx])
            cur_idx += 1
    for num in b:
        print(num, end=' ')
    print()
solve()

Solution (Python):
def main():
    test = 1
    for _ in range(test):
        n = int(input())
        ara = [int(num) for num in input().split()]
        mark = [True for _ in range(n + 1)]
        for num in ara:
            mark[num] = False
        take_from = []
        for index in range(n + 1):
            if mark[index]:
                take_from.append(index)
        ans = []
        ans.append(take_from[0])
        take_at = 1
        take_size = len(take_from)
        for index in range(1, n):
            if ara[index] != ara[index - 1]:
                ans.append(ara[index - 1])
            elif take_at < take_size:
                ans.append(take_from[take_at])
                take_at += 1
            else:
                ans.append(n + 1)
        ans = ' '.join(map(str, ans))
        print(ans)
if __name__ == '__main__':
    main()