CF BUDDY
← Problems·

1969E · Unique Array

2400 · binary search, data structures, divide and conquer

Problem: You are given an integer array aa of length nn. A subarray of aa is one of its contiguous subsequences (i. e. an array [al,al+1,,ar][a_l, a_{l+1}, \dots, a_r] for some integers ll and rr such that 1l<rn1 \le l < r \le n). Let's call a subarray unique if there is an integer that occurs exactly once in the subarray.

You can perform the following operation any number of times (possibly zero): choose an element of the array and replace it with any integer.

Your task is to calculate the minimum number of aforementioned operation in order for all the subarrays of the array aa to be unique.

Input Format: The first line contains a single integer tt (1t1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains a single integer nn (1n31051 \le n \le 3 \cdot 10^5).

The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (1ain1 \le a_i \le n).

Additional constraint on the input: the sum of nn over all test cases doesn't exceed 31053 \cdot 10^5.

Output Format: For each test case, print a single integer — the minimum number of aforementioned operation in order for all the subarrays of the array aa to be unique.

Note: In the second test case, you can replace the 11-st and the 33-rd element, for example, like this: [3,4,1,4][3, 4, 1, 4].

In the third test case, you can replace the 44-th element, for example, like this: [3,1,2,3,2][3, 1, 2, 3, 2].

Sample Cases

Case 1

Input

4
3
2 1 2
4
4 4 4 4
5
3 1 2 1 2
5
1 3 2 1 2

Output

0
2
1
0

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback