Even Array

简介: Even Array

文章目录

一、B. Even Array

总结


一、B. Even Array

本题链接:B. Even Array


题目:


B. Even Array


time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output


You are given an array a[0…n−1] of length n 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 i (0≤i≤n−1) the equality imod2=a[i]mod2 holds, where xmod2 is the remainder of dividing x by 2.


For example, the arrays [0,5,2,1] and [0,17,0,3] are good, and the array [2,4,6,7] is bad, because for i=1, the parities of i and a[i] are different: imod2=1mod2=1, but a[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 a good, or say that this is not possible.


Input

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


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


The next line contains n integersa0,a1,…,an−1 (0≤ai≤1000) — the initial array.


Output

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


Example

input

4

4

3 2 7 6

3

3 2 6

1

7

7

4 9 2 1 18 3 0

output

2

1

-1

0

Note

In the first test case, in the first move, you can swap the elements with indices 0 and 1, and in the second move, you can swap the elements with indices 2 and 3.


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


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


本博客给出本题截图:

7.png

题意:要求数组从0开始,奇数的索引必须元素为奇数,偶数的索引必须元素为偶数,问对于一个数组,每次可以交换任意两个位置上的元素,问最少交换几次可以使数组满足上述需求,如果不可以,输出-1

AC代码

#include <iostream>
using namespace std;
const int N = 1010;
int a[N];
int main()
{
    int t;
    cin >> t;
    while (t -- )
    {
        int n, odd = 0, even = 0;
        cin >> n;
        for (int i = 0; i < n; i ++ )
        {
            cin >> a[i];
            if (i % 2 == 0 && a[i] % 2 != 0)
                odd ++;
            else if (i % 2 != 0 && a[i] % 2 == 0)
                even ++;
        }
        if (odd != even)
            puts("-1");
        else 
            cout << odd << endl;
    }
    return 0;
}

总结

水题,不解释

目录
相关文章
|
7月前
Array.from() 与 Array.reduce()
Array.from() 与 Array.reduce()
46 1
|
3月前
|
JavaScript 前端开发 索引
|
7月前
|
存储 机器学习/深度学习 JavaScript
array
array
48 2
|
JavaScript 前端开发 索引
Array类型【find】
Array类型【find】
100 0
|
索引
Array 数组
Array 数组
90 0
|
安全 编译器 C++
认识、使用C++vector和array
认识、使用C++vector和array
152 0
|
存储
Array
Array
144 0
|
开发者 索引 Python
Array 数组|学习笔记
快速学习 Array 数组
134 0
|
存储 索引
你真的用对 Array.map() 了吗
你真的用对 Array.map() 了吗
152 0
|
Java C# 索引
C# 数组(Array)
基础知识 声明数组 datatype[] arrayName; 初始化数组: 声明一个数组不会在内存中初始化数组。当初始化数组变量时,您可以赋值给数组。 数组是一个引用类型,所以您需要使用 new 关键字来创建数组的实例。 例如: double[] balance = new double[10]; 您可以在声明数组的同时给数组赋值,比如: double[] balance = { 2340.0, 4523.69, 3421.0}; 您也可以创建并初始化一个数组,比如: int[] marks = new int[5] { 99, 98, 92, 97, 95}; 在上述情况下,你
219 0