Constructive Problem (Codeforces 1820C)
Tags: Greedy, constructive algorithms

As you know, any problem that does not require the use of complex data structures is considered constructive. You are offered to solve one of such problems.You are given an array $$$a$$$ of $$$n$$$ non-negative integers. You are allowed to perform the following operation exactly once: choose some non-empty subsegment $$$a_l, a_{l+1}, \ldots, a_r$$$ of the array $$$a$$$ and a non-negative integer $$$k$$$, and assign value $$$k$$$ to all elements of the array on the chosen subsegment.The task is to find out whether $$$\operatorname{MEX}(a)$$$ can be increased by exactly one by performing such an operation. In other words, if before the operation $$$\operatorname{MEX}(a) = m$$$ held, then after the operation it must hold that $$$\operatorname{MEX}(a) = m + 1$$$.Recall that $$$\operatorname{MEX}$$$ of a set of integers $$$c_1, c_2, \ldots, c_k$$$ is defined as the smallest non-negative integer $$$x$$$ which does not occur in the set $$$c$$$.

Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 50\,000$$$) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 200\,000$$$) — the number of elements of array $$$a$$$.The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) — elements of array $$$a$$$.It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$200\,000$$$.

Output: For each test case, print "Yes" if you can increase $$$\operatorname{MEX}(a)$$$ by exactly one by performing the operation from the statement exactly once, otherwise print "No".You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

Note: In the first test case, $$$\operatorname{MEX}(a) = 0$$$. If you set all elements of $$$a$$$ to $$$0$$$, then $$$\operatorname{MEX}$$$ of the resulting array will be $$$1$$$, and thus will increase by one.In the second test case, $$$\operatorname{MEX}(a) = 1$$$. If we assign a value of $$$1$$$ to the elements of $$$a$$$ on a subsegment from $$$2$$$ to $$$3$$$, we get an array $$$[0, 1, 1, 0]$$$ for which $$$\operatorname{MEX}$$$ is $$$2$$$, and thus is increased by one compared to the original.It can be shown that in the third and fourth test cases it is impossible to perform an operation so that the value of $$$\operatorname{MEX}(a)$$$ increases by exactly one.

Sample Inputs:
4
3
1 2 1
4
0 2 2 0
4
3 2 0 2
1
0

Sample Outputs:
Yes
Yes
No
No

Solution (Python):
import sys
inp = sys.stdin.readline
for _ in range(int(inp())):
    n = int(inp())
    A = [*map(int, inp().split())]
    S = set(A)
    mex = 0
    for i in range(200000):
        if i not in S:
            mex = i
            break
    X = []
    for i in range(n):
        if A[i] == mex + 1:
            X.append(i)
    if X != []:
        for i in range(min(X), max(X) + 1):
            A[i] = mex
        S = set(A)
        for i in range(mex):
            if i not in S:
                print('NO')
                break
        else:
            print('YES')
    elif mex == n:
        print('NO')
    else:
        print('YES')

Solution (Python):
import sys
input = sys.stdin.readline

def readList():
    return list(map(int, input().split()))

def readInt():
    return int(input())

def readInts():
    return map(int, input().split())

def readStr():
    return input().strip()

def solve():
    n = readInt()
    arr = readList()
    a2 = sorted(arr)
    if a2[0] > 0:
        return 'YES'
    (t, idx) = (0, -1)
    for i in range(n):
        if a2[i] == t:
            continue
        if t + 1 == a2[i]:
            t += 1
        else:
            idx = i
            break
    mex = t + 1
    if idx == -1:
        return 'NO' if a2[-1] == n - 1 else 'YES'
    v = a2[idx]
    if mex + 1 < v:
        return 'YES'
    tmp = []
    others = []
    for i in range(n):
        if arr[i] == v:
            tmp.append(i)
    for j in range(n):
        if j < tmp[0] or j > tmp[-1]:
            others.append(arr[j])
    t = 0
    others.sort()
    m = len(others)
    for i in range(m):
        if t == others[i]:
            continue
        if t + 1 == others[i]:
            t += 1
        else:
            break
    mex2 = t + 1 if others and others[0] == 0 else 0
    if mex2 == mex:
        return 'YES'
    else:
        return 'NO'
for _ in range(int(input())):
    print(solve())

Solution (Python):
for _ in range(int(input())):
    n = int(input())
    arr = [int(i) for i in input().split()]
    data1 = {}
    data2 = {}
    for (i, e) in enumerate(arr):
        if e not in data1:
            data1[e] = i
        data2[e] = i
    mex = 0
    while mex in data1:
        mex += 1
    if mex + 1 not in data1:
        if mex == n:
            print('No')
        else:
            print('Yes')
        continue
    for i in range(data1[mex + 1], data2[mex + 1] + 1):
        arr[i] = mex
    data3 = {}
    s = set(arr)
    res = 0
    while res in s:
        res += 1
    if res == mex + 1:
        print('Yes')
    else:
        print('No')