Remove Prefix (Codeforces 1714B)
Tags: Greedy, data structures

Polycarp was presented with some sequence of integers $$$a$$$ of length $$$n$$$ ($$$1 \le a_i \le n$$$). A sequence can make Polycarp happy only if it consists of different numbers (i.e. distinct numbers).In order to make his sequence like this, Polycarp is going to make some (possibly zero) number of moves.In one move, he can:   remove the first (leftmost) element of the sequence. For example, in one move, the sequence $$$[3, 1, 4, 3]$$$ will produce the sequence $$$[1, 4, 3]$$$, which consists of different numbers.Determine the minimum number of moves he needs to make so that in the remaining sequence all elements are different. In other words, find the length of the smallest prefix of the given sequence $$$a$$$, after removing which all values in the sequence will be unique.

Input: The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.Each test case consists of two lines.The first line contains an integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the given sequence $$$a$$$.The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — elements of the given sequence $$$a$$$.It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$2 \cdot 10^5$$$.

Output: For each test case print your answer on a separate line — the minimum number of elements that must be removed from the beginning of the sequence so that all remaining elements are different.

Note: The following are the sequences that will remain after the removal of prefixes:  $$$[1, 4, 3]$$$;  $$$[1]$$$;  $$$[1]$$$;  $$$[6, 5, 4, 3, 2, 1]$$$;  $$$[2, 1]$$$. It is easy to see that all the remaining sequences contain only distinct elements. In each test case, the shortest matching prefix was removed.

Sample Inputs:
5
4
3 1 4 3
5
1 1 1 1 1
1
1
6
6 5 4 3 2 1
7
1 2 1 7 1 2 1

Sample Outputs:
1
4
0
0
5

Solution (Python):
def unique_breaks(s):
    r = {}
    start = len(s) - 1
    end = -1
    step = end
    for k in range(start, end, step):
        v = s[k]
        if r.get(v) == None:
            r[v] = 1
        else:
            return k + 1
    return 0

def main():
    t = input()
    t = int(t)
    from itertools import repeat
    b = list(repeat(0, t))
    for i in range(t):
        n = input()
        s = input()
        b[i] = unique_breaks(s.split(' '))
    for k in range(t):
        print(b[k])
if __name__ == '__main__':
    main()
''

Solution (Python):
""""""

def Solve():
    (ns, A) = ([], [])
    for t in range(int(input())):
        ns.append(int(input()))
        A.append(list(map(int, input().split())))
    for (n, a) in zip(ns, A):
        s = set()
        for i in a[::-1]:
            if i not in s:
                s.add(i)
            else:
                print(n - len(s))
                break
        else:
            print(0)
if __name__ == '__main__':
    Solve()

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
mis = lambda : map(int, input().split())
INF = float('inf')

def solve():
    N = int(input())
    arr = list(mis())
    cnt = [0] * (N + 1)
    ans = N
    for i in range(N - 1, -1, -1):
        if cnt[arr[i]]:
            break
        ans -= 1
        cnt[arr[i]] = 1
    print(ans)
if __name__ == '__main__':
    T = int(input())
    for _ in range(T):
        solve()