A. Unit Array | Codeforces Solution | Codeforces Round 879 (Div. 2)

Raunit Verma - in Codeforces
Views: 24

Given an array š‘Ž of length š‘› , which elements are equal to āˆ’1 and 1 . Let's call the array š‘Ž good if the following conditions are held at the same time: š‘Ž1+š‘Ž2+…+š‘Žš‘›ā‰„0 ; š‘Ž1ā‹…š‘Ž2ā‹…ā€¦ā‹…š‘Žš‘›=1 . In one operation, you can select an arbitrary element of the array š‘Žš‘– and change its value to the opposite. In other words, if š‘Žš‘–=āˆ’1 , you can assign the value to š‘Žš‘–:=1 , and if š‘Žš‘–=1 , then assign the value to š‘Žš‘–:=āˆ’1 .

A. Unit Array

time limit per test: 1 second | memory limit per test: 256 megabytes

Given an array a of length n, which elements are equal to āˆ’1 and 1. Let's call the array a good if the following conditions are held at the same time:

  • a1 + a2 + … + an ≄ 0;
  • a1 ā‹… a2 ā‹… … ā‹… an = 1.

In one operation, you can select an arbitrary element of the array ai and change its value to the opposite. In other words, if ai = āˆ’1, you can assign the value to ai := 1, and if ai = 1, then assign the value to ai := āˆ’1.

Determine the minimum number of operations you need to perform to make the array a good. It can be shown that this is always possible.

Input

Each test consists of multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 500) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n (1 ≤ n ≤ 100) — the length of the array a.

The second line of each test case contains n integers a1, a2, …, an (ai = ±1) — the elements of the array a.

Output

For each test case, output a single integer — the minimum number of operations that need to be done to make the a array good.

Example

Input

7
4
-1 -1 1 -1
5
-1 -1 -1 1 1
4
-1 1 -1 1
3
-1 -1 -1
5
1 1 1 1 1
1
-1
2
-1 -1

Output

1
1
0
3
0
1
2

Note

In the first test case, we can assign the value a1 := 1. Then a1 + a2 + a3 + a4 = 1 + (āˆ’1) + 1 + (āˆ’1) = 0 ≄ 0 and a1 ā‹… a2 ā‹… a3 ā‹… a4 = 1 ā‹… (āˆ’1) ā‹… 1 ā‹… (āˆ’1) = 1. Thus, we performed 1 operation.

In the second test case, we can assign a1 := 1. Then a1 + a2 + a3 + a4 + a5 = 1 + (āˆ’1) + (āˆ’1) + 1 + 1 = 1 ≄ 0 and a1 ā‹… a2 ā‹… a3 ā‹… a4 ā‹… a5 = 1 ā‹… (āˆ’1) ā‹… (āˆ’1) ā‹… 1 ā‹… 1 = 1. Thus, we performed 1 operation.

In the third test case, a1 + a2 + a3 + a4 = (āˆ’1) + 1 + (āˆ’1) + 1 = 0 ≄ 0 and a1 ā‹… a2 ā‹… a3 ā‹… a4 = (āˆ’1) ā‹… 1 ā‹… (āˆ’1) ā‹… 1 = 1. Thus, all conditions are already satisfied and no operations are needed.

In the fourth test case, we can assign the values a1 := 1, a2 := 1, a3 := 1. Then a1 + a2 + a3 = 1 + 1 + 1 = 3 ≄ 0 and a1 ā‹… a2 ā‹… a3 = 1 ā‹… 1 ā‹… 1 = 1. Thus, we performed 3 operations.

void solve()
{

  int n;
  cin >> n;

  int neg = 0;

  for (int i = 0; i < n; i++)
  {
    int x;
    cin >> x;
    if (x == -1)
      neg++;
  }

  int pos = n - neg;

  if (pos >= neg && neg % 2 == 0)
  {
    cout << 0 << endl;
  }
  else if (pos >= neg && neg % 2)
  {
    cout << 1 << endl;
  }
  else
  {
    int diff = neg - pos;
    int change = diff % 2 + diff / 2;
    neg -= change;
    if (neg % 2 == 0)
    {
      cout << change << endl;
    }
    else
      cout << change + 1 << endl;
  }

  // neg = 7 pos = 4 diff = -3
  // neg = 6 pos = 5 diff = -1
  // neg = 5 pos = 6 diff = 1

  // neg = 8 pos = 4 diff = -4
  // neg = 7 pos = 5 diff = -3
  // neg = 6 pos = 6 diff = 0
  //
}
TagsA. Unit ArrayCodeforces Round 879 (Div. 2)Codeforces Solution
Raunit Verma-picture
Raunit Verma

Technical Writer at CodingKaro

Share this on
CodingKaro App Poster
CodingKaro App
4.7

3K+ Downloads

One of its unique features is an automated coding contest reminder, which sets alarms for upcoming coding contests on popular platforms like CodeChef, CodeForces, and LeetCode, without the need for user input. This feature ensures that users never miss a coding contest and can stay updated with the latest coding challenges.

Download CodingKaro AppDownload CodingKaro App
Other Blogs in Codeforces