Good Sequence Codechef solutions Today | Codechef Starters 72 ✅ (100/100) FULL | AC Code ✅| GSEQ
Problem
Suppose you have a binary array of length .
A sequence is called good with respect to if it satisfies the following conditions:
- For every pair such that , the subarray contains more ones than zeros.
- That is, if contains ones and zeros, then must hold.
Here, denotes the subarray consisting of elements .
Note that in particular, a sequence of size is always good.
For example, suppose . Then,
- The sequence is a good sequence. The subarrays that need to be checked are and , which all satisfy the condition.
- The sequence is not good, because contains an equal number of zeros and ones (when it should contain one extra ).
Alice gave Bob a binary array of size and asked him to find the longest sequence that is good with respect to . Help Bob find one such sequence.
If multiple possible longest sequences exist, you may print any of them.
Input Format
- The first line of input will contain a single integer , denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains a single integer — the size of the binary array.
- The second line contains space-separated numbers — .
Output Format
Each test case requires two lines of output:
- First, print on a new line a single integer — the maximum length of a sequence that is good with respect to
- On the next line, print space-separated integers in increasing order, denoting the indices of one such sequence.
If there are multiple possible good sequences with maximum size, output any of them.
Constraints
- The sum of over all test cases won't exceed .
Sample 1:
4 6 0 1 1 0 1 1 5 0 1 0 1 0 4 1 1 1 1 3 1 0 0
4 2 5 6 7 2 4 5 5 1 2 3 4 5 2 1 2
Explanation:
Test case : We have . The sequence requires us to check subarrays:
- which has and corresponds to
- which has and corresponds to
- which has and corresponds to
- , which has and corresponds to
- , which has and corresponds to
- , which has and corresponds to
As you can see, all pairs satisfy the condition.
Test case : The only subarray that needs to be checked is , which has as needed. It can be verified that no good sequence of length greater than exists.