Adjacent Parity

3.2

6 votes
Linear Search, Algorithms, Greedy algorithm,
Problem

You are given an array A containing N integers. Find if it is possible to rearrange the elements of the array such that the parity of the sum of each pair of adjacent elements is equal (formally, parity(Ai+Ai+1)=parity(Aj+Aj+1) for each 1i<jN1 ).

Here, parity refers to the remainder obtained when a number is divided by 2 (i.e. parity(x)=xmod2).

Input format

  • The first line contains denoting the number of test cases. The description of T test cases is as follows:
  • For each test case:
    • The first line contains denoting the size of array A.
    • The second line contains N space-separated integers A[1], A[2], ....., A[N] - denoting the elements of A.

Output format
For each test case, print "YES"(without quotes) if it is possible to reorder the array elements to satisfy the given condition and "NO"(without quotes) otherwise.

Constraints

1T1001N1001Ai100

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The first test case

  • The given array already satisfies the required condition. Here pairty(Ai+Ai+1)=0 for each 1i2.

The second test case

  • There is no way to reorder the elements of the given array to satisfy the required condition.

The third test case

  • One possible reordering is [5, 2, 7, 2, 1].  Here pairty(Ai+Ai+1)=1 for each 1i4.
Editor Image

?