B. Even Array

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array a[0…n−1]a[0…n−1] of length nn which consists of non-negative integers. Note that array indices start from zero.

An array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all ii (0≤i≤n−10≤i≤n−1) the equality imod2=a[i]mod2imod2=a[i]mod2 holds, where xmod2xmod2 is the remainder of dividing xx by 2.

For example, the arrays [0,5,2,10,5,2,1] and [0,17,0,30,17,0,3] are good, and the array [2,4,6,72,4,6,7] is bad, because for i=1i=1, the parities of ii and a[i]a[i] are different: imod2=1mod2=1imod2=1mod2=1, but a[i]mod2=4mod2=0a[i]mod2=4mod2=0.

In one move, you can take any two elements of the array and swap them (these elements are not necessarily adjacent).

Find the minimum number of moves in which you can make the array aa good, or say that this is not possible.

Input

The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of test cases in the test. Then tt test cases follow.

Each test case starts with a line containing an integer nn (1≤n≤401≤n≤40) — the length of the array aa.

The next line contains nn integers a0,a1,…,an−1a0,a1,…,an−1 (0≤ai≤10000≤ai≤1000) — the initial array.

Output

For each test case, output a single integer — the minimum number of moves to make the given array aa good, or -1 if this is not possible.

Example

input

Copy

4
4
3 2 7 6
3
3 2 6
1
7
7
4 9 2 1 18 3 0

output

Copy

2
1
-1
0

Note

In the first test case, in the first move, you can swap the elements with indices 00 and 11, and in the second move, you can swap the elements with indices 22 and 33.

In the second test case, in the first move, you need to swap the elements with indices 00 and 11.

In the third test case, you cannot make the array good.

解題說明:此題其實就是判斷數列中奇偶數情況,遍歷時進行統計即可。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>

using namespace std;

int main()
{
	int t, n, a[1001], c1 = 0, c2 = 0, j, i;
	scanf("%d", &t);
	for (i = 0; i<t; ++i)
	{
		c1 = 0;
		c2 = 0;
		scanf("%d", &n);
		for (j = 0; j<n; ++j)
		{
			scanf("%d", &a[j]);
			if (j % 2 != a[j] % 2)
			{
				if (j % 2 == 0)
				{
					c1++;
				}
				if (j % 2 == 1)
				{
					c2++;
				}
			}
		}
		if (c1 == c2)
		{
			printf("%d\n", c1);
		}
		else
		{
			printf("-1\n");
		}
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章