Divide arrays

2.5

63 votes
Algorithms, Basics of Greedy Algorithms, C++, Greedy Algorithms
Problem

You are given an array A of N integers. Find the smallest index i (1iN1) 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

1T101N1050A[i]105

Note

  • 1-based Indexing is followed.
  • mex of an array of elements is defined as the minimum non-negative number missing from the array.
Sample Input
1
5
0 2 2 3 0
Sample Output
1
Time Limit: 3
Memory Limit: 256
Source Limit:
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.

Editor Image

?