Divide arrays
Algorithms
Basics of greedy algorithms
C++
Greedy algorithms
87.0% success
11,306 attempts
3s time
256MB memory
1024KB max code
You are given an array \(A\) of \(N\) integers. Find the smallest index \(i\) \((1 \le i \le N - 1)\) such that:
- \(mex(A[1], A[2], .. , A[i]) = mex(A[i+1], A[i+2],... , A[N])\)
If no such index exists, print \(-1\).
Input format
- The first line contains an integer \(T\) denoting the number of test cases.
- For each test case:-
- The first line of each test case contains an integer \(N\) denoting the number of elements in array \(A\).
- The second line contains \(N\) space-separated integers denoting the elements of the array.
Output format
For each test case in a new line, print the smallest index satisfying the condition.
Constraints
\(1 \le T \le 10\)
\(1 \le N \le 10^5\)
\(0 \le A[i] \le 10^5\)
Note
- 1-based Indexing is followed.
- \(mex\) of an array of elements is defined as the minimum non-negative number missing from the array.
Examples
Input
1 5 0 2 2 3 0
Output
1
Explanation
If \(i = 1\),
- \(mex(A[1]) = mex(0) = 1\)
- \(mex(A[2], A[3], A[4], A[5]) = mex(2,2,3,0) = 1\)
Hence, index \(1\) is required answer.
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Loading Editor...