PROBLEM LINK:
Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4
Author: utkarsh_25dec
Testers: nishant403, satyam_343
Editorialist: iceknight1093
DIFFICULTY:
TBD
PREREQUISITES:
None
PROBLEM:
You’re given an array B of length N, where B_i denotes the parity of the number of occurrences of element i in array A, where A has length N.
Does a valid array A exist?
EXPLANATION:
Let’s start with an empty array A and try to build a valid one as we go.
For each i from 1 to N,
- If B_i = 1, there are an odd number of occurrences of i in A. In particular, A must contain at least one occurrence of i, so let’s add exactly one for now.
- If B_i = 0, there are an even number of occurrences of i in A. Let’s ignore this i for now.
Our process gives us some array A of length \leq N, since it contains at most one occurrence of each i between 1 and N
Notice that we can extend it to length N by adding some more elements, but in order to maintain parity, we can only add an even number of any element.
So, if the remaining length is even, the answer is “Yes”; otherwise the answer is “No”.
This has a rather simple formulation in terms of the input: the initial length of the array we create is exactly the number of ones in B, which equals sum(B) since B is a boolean array.
So, all that needs to be done is to check whether sum(B) and N have the same parity or not!
TIME COMPLEXITY:
\mathcal{O}(N) per testcase.
CODE:
Code (Python)
for _ in range(int(input())):
n = int(input())
b = list(map(int, input().split()))
print('Yes' if sum(b)%2 == n%2 else 'No')