Read problem statements in [Russian]

Chef has a sequence $A_{1}, A_{2}, \ldots, A_{N}$. In one operation, Chef can choose one index $i$ ($1 ≤ i ≤ N$) and change $A_{i}$ to $A_{i}^{p}$, where $p = \max(0, {\lceil \frac{A_{i}}{2} \rceil} - 1)$.

Help Chef find the smallest number of operations needed to make the sum of the sequence even or determine that it is impossible.

------  Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.

------  Output ------
For each test case, print a single line containing one integer — the minimum number of operations needed to make the sum of the sequence even, or $-1$ if it is impossible.

------  Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$

------  Subtasks ------
Subtask #1 (30 points):
$N ≤ 100$
$1 ≤ A_{i} ≤ 10$ for each valid $i$

Subtask #2 (70 points): original constraints

----- Sample Input 1 ------ 
4

4

7 3 5 1

5

4 2 7 8 10

2

9 9

3

1 1 1
----- Sample Output 1 ------ 
0

1

0

-1
----- explanation 1 ------ 
Example case 1: The sum of the sequence is $16$, which is already even.

Example case 2: We can change $A_{2}$ to $1$, so that the sequence becomes $[4, 1, 7, 8, 10]$. The sum of this sequence is $30$, which is even.

Example case 3: The sum of the sequence is $18$, which is already even.

Example case 4: It is impossible to make the sum of the sequence even.

Source: codechef | Difficulty: MEDIUM
Tags: Mathematics


Solutions:
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if sum(a) % 2 == 0:
		print(0)
	else:
		c = 0
		for i in range(n):
			if a[i] == 2:
				print(1)
				c = 1
				break
		if c == 0:
			print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().lstrip().split()))
	odd = []
	even = []
	for i in range(n):
		if arr[i] % 2 == 0:
			even.append(arr[i])
		else:
			odd.append(arr[i])
	if len(odd) % 2 == 0:
		print(0)
	elif len(even) == 0 and len(odd) % 2 == 1:
		print(-1)
	elif 2 in even:
		print(1)
	else:
		print(-1)


import sys
for i in range(int(input())):
	n = int(input())
	a = list(map(int, sys.stdin.readline().strip().split()))
	if sum(a) % 2 == 0:
		print(0)
	elif 2 in a:
		print(1)
	else:
		print(-1)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if sum(a) % 2 == 0:
		print(0)
	elif 2 in a:
		print(1)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	odd = 0
	two = False
	for i in a:
		if i % 2 != 0:
			odd += 1
		elif i == 2:
			two = True
	if odd % 2 != 0:
		if two:
			print(1)
		else:
			print(-1)
	else:
		print(0)


t = int(input())
for j in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if sum(a) % 2 == 0:
		print(0)
	else:
		a = a.count(2)
		if a > 0:
			print(1)
		else:
			print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = l[:]
	c = 0
	if sum(l1) % 2 == 0:
		print(0)
	elif 2 in l:
		print(1)
	else:
		print(-1)


T = int(input())
for i in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	if sum(A) % 2 == 0:
		print(0)
	elif sum(A) % 2 == 1 and 2 in A:
		print(1)
	else:
		print(-1)


T = int(input())
for i in range(T):
	N = int(input())
	A = [int(i) for i in input().split(' ')]
	e = 0
	o = 0
	for i in A:
		if i % 2 != 0:
			o = o + 1
		else:
			e = e + 1
	A.sort()
	if o % 2 != 0:
		if e > 0:
			for i in A:
				if i % 2 == 0 and i // 2 - 1 != 0:
					print(-1)
					break
				elif i % 2 == 0:
					print(1)
					break
		else:
			print(-1)
	else:
		print(0)


t = int(input())
while t > 0:
	n = int(input())
	arr = list(map(int, input().split()))
	if sum(arr) % 2 == 0:
		print('0')
	elif sum(arr) != 0 and 2 in arr:
		print('1')
	else:
		print('-1')
	t = t - 1


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = sum(a)
	if s % 2 == 0:
		print('0')
	elif s % 2 == 1 and 2 in a:
		print('1')
	else:
		print('-1')


for _ in range(int(input())):
	b = int(input())
	a = list(map(int, input().split()))
	c = 0
	for i in range(len(a)):
		c += a[i]
	if c % 2 == 0:
		print('0')
	else:
		c = 0
		for i in range(len(a)):
			if a[i] == 2:
				a[i] = a[i] - 1
				break
		for i in range(len(a)):
			c += a[i]
		if c % 2 == 0:
			print('1')
		else:
			print('-1')


t = int(input())
for i in range(t):
	m = int(input())
	arr = [int(x) for x in input().split()]
	if sum(arr) % 2 == 0:
		print('0')
	elif sum(arr) % 2 != 0 and 2 in arr:
		print('1')
	else:
		print('-1')


t = int(input())
for test in range(t):
	n = int(input())
	lst = list(map(int, input().split()))[:n]
	if sum(lst) % 2 == 0:
		print(0)
	elif sum(lst) % 2 != 0 and 2 in lst:
		print(1)
	else:
		print(-1)


def isEvenn(list):
	sum = 0
	for i in list:
		sum += i
	return sum % 2 == 0

def contains2(list):
	result = False
	for i in list:
		if i == 2:
			result = True
			break
	return result

def parseInput():
	t = int(input())
	testCases = []
	for i in range(t):
		n = int(input())
		tc = input()
		testCases.append(list((int(x) for x in tc.split())))
	return testCases

def printResult(result: list):
	for i in result:
		print(i)

def main():
	testCases = parseInput()
	result = []
	for case in testCases:
		if isEvenn(case):
			result.append(0)
			continue
		result.append(1 if contains2(case) else -1)
	printResult(result)
main()


t = int(input())
for i in range(t):
	n = int(input())
	A = [int(i) for i in input().split(' ')]
	e = 0
	o = 0
	for i in A:
		if i % 2 != 0:
			o = o + 1
		else:
			e = e + 1
	A.sort()
	if o % 2 != 0:
		if e > 0:
			for i in A:
				if i % 2 == 0 and i // 2 - 1 != 0:
					print(-1)
					break
				elif i % 2 == 0:
					print(1)
					break
		else:
			print(-1)
	else:
		print(0)


t = int(input())
for i in range(t):
	x = int(input())
	s = list(map(int, input().split()))
	q = sum(s)
	if q % 2 == 0:
		print('0')
	elif 2 in s:
		print('1')
	else:
		print('-1')


T = int(input())
for i in range(T):
	N = int(input())
	l = list(map(int, input().split()))
	if sum(l) % 2 == 0:
		print(0)
	elif l.count(2) > 0:
		print(1)
	else:
		print(-1)
print()


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if sum(l) % 2 == 0:
		print('0')
	elif 2 in l:
		print(1)
	else:
		print('-1')


T = int(input())
for i in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	e = 0
	o = 0
	t = 0
	for i in A:
		if i % 2 == 0:
			e = e + 1
		elif i % 2 != 0:
			o = o + 1
		if i == 2:
			t = t + 1
	if o % 2 == 0:
		print('0')
	elif o % 2 != 0:
		if t >= 1:
			print('1')
		else:
			print('-1')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	flag = False
	sum_ = 0
	for i in a:
		sum_ ^= i & 1
		if i == 2:
			flag = True
	if not sum_:
		print(0)
	elif flag:
		print(1)
	else:
		print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	even = 0
	for j in l:
		if j % 2 == 0:
			even += 1
	odd = n - even
	if odd % 2 == 0:
		print(0)
	elif 2 in l:
		print(1)
	else:
		print(-1)


from math import ceil
t = int(input())
for _ in range(t):
	n = int(input())
	l = [int(x) for x in input().split()]
	if not sum(l) % 2:
		print(0)
	elif l.count(2) > 0:
		print(1)
	else:
		print(-1)


t = int(input())
for tc in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	if sum(a) % 2 == 0:
		print('0')
	elif sum(a) % 2 != 0 and 2 in a:
		print('1')
	else:
		print('-1')


T = int(input())
for _ in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	B = sum(A)
	if B % 2 == 0:
		print(0)
	else:
		C = 0
		for i in range(N):
			if A[i] == 2:
				C += 1
				break
		if C != 0:
			print(1)
		else:
			print(-1)


import sys
input = sys.stdin.readline

def rii():
	return range(int(input().strip()))

def ii():
	return int(input().strip())

def mii():
	return map(int, input().strip().split(' '))

def lmii():
	return list(map(int, input().strip().split(' ')))

def si():
	return str(input().strip())

def solve(*args):
	(n, a) = args
	if sum(a) % 2 == 0:
		return 0
	elif 2 in a:
		return 1
	else:
		return -1

def do_codechef():
	for t in rii():
		n = ii()
		a = lmii()
		print(solve(n, a))
do_codechef()
exit()


try:
	for _ in range(int(input())):
		n = int(input())
		arr = list(map(int, input().split()))
		odds = 0
		twos = 0
		if sum(arr) % 2 == 0:
			print(0)
		elif 2 in arr:
			print(1)
		else:
			print(-1)
except:
	pass


t = int(input())
for _ in range(t):
	u = int(input())
	l = list(map(int, input().split()))
	p = sum(l)
	if p % 2 == 0:
		print(0)
	elif 2 in l:
		print(1)
	else:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	if sum(arr) % 2 == 0:
		print('0')
	elif arr.count(2) >= 1:
		print('1')
	else:
		print('-1')


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	s = 0
	c = 0
	for j in l:
		if j % 2 != 0:
			c += 1
		elif j == 2:
			s = 1
	print(0 if c % 2 == 0 else 1 if s == 1 else -1)


for _ in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	f = c = 0
	for ele in li:
		if ele % 2 != 0:
			c = c + 1
		elif ele == 2:
			f = 1
	if c % 2 == 0:
		print(0)
	elif f == 1:
		print(1)
	else:
		print(-1)


for abcd in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	even = s = 0
	for ele in li:
		s += ele
		if ele == 2:
			even += 1
	if s % 2 == 0:
		print(0)
		continue
	if even != 0:
		print(1)
		continue
	print(-1)


a = int(input())
for i in range(a):
	b = int(input())
	c = list(map(int, input().split()))
	s = sum(c)
	t = c.count(2)
	if s % 2 == 0:
		print(0)
	elif s % 2 == 1 and t != 0:
		print(1)
	else:
		print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	odds = 0
	for i in range(len(a)):
		if a[i] % 2 != 0:
			odds += 1
	if odds % 2 == 0:
		print(0)
	elif 2 in a:
		print(1)
	else:
		print(-1)


queries = int(input())
while queries > 0:
	queries -= 1
	n = int(input())
	a = list(map(int, input().split()))
	if sum(a) % 2 == 0:
		ans = 0
	else:
		for i in a:
			if i == 2:
				ans = 1
				break
		else:
			ans = -1
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().strip().split()))[:n]
	zerr = None
	sum_arr = sum(arr)
	if sum_arr % 2 == 0:
		zerr = True
	else:
		for j in range(len(arr)):
			counter = arr[j] / 2 - 1
			if arr[j] % 2 == 0 and counter == 0:
				zerr = False
	if zerr:
		print(0)
	elif zerr == False:
		print(1)
	else:
		print(-1)


for t in range(int(input())):
	n = int(input())
	numList = list(map(int, input().split()))
	sumList = sum(numList)
	if sumList % 2 == 0:
		print(0)
	elif 2 not in numList:
		print(-1)
	else:
		print(1)


T = int(input())
for i in range(T):
	N = int(input())
	Nnum = list(map(int, input().split()))
	s = sum(Nnum)
	if s % 2 == 0:
		print(0)
	elif 2 in Nnum:
		print(1)
	else:
		print(-1)


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	c = []
	for i in range(n):
		if a[i] % 2 == 1:
			c.append(a[i])
	if len(c) % 2 == 0:
		print(0)
	elif a.__contains__(2):
		print(1)
	else:
		print(-1)
try:
	t = int(input())
	for i in range(t):
		solve()
except:
	pass


import sys
MOD = 1000000007
inp = sys.stdin.readline

def ii():
	return int(inp())

def iis():
	return [int(i) for i in inp().split()]
for t in range(ii()):
	n = ii()
	nums = iis()
	_s = sum(nums)
	if _s % 2 == 0:
		print(0)
	elif 2 in nums:
		print(1)
	else:
		print(-1)


try:
	for _ in range(int(input())):
		N = int(input())
		A = [int(x) for x in input().split()]
		s = 0
		for i in A:
			s += i
		if s % 2 == 0:
			print(0)
		elif 2 in A:
			print(1)
		else:
			print(-1)
except:
	pass


import array as arr
t = int(input())
for i in range(t):
	n = int(input(''))
	sum = 0
	cn = 0
	a = list(map(int, input().split()))
	for j in range(n):
		sum = sum + a[j]
	if sum % 2 == 0:
		print('0\n')
	else:
		for k in range(0, n):
			m = a[k]
			if m % 2 == 0 and m <= 2:
				cn = 1
				break
			else:
				cn = -1
		print(cn, '\n')


n = int(input())
for i in range(n):
	n1 = int(input())
	s = 0
	l1 = list(map(int, input().split()))
	p = sum(l1)
	if p % 2 == 0:
		print(0)
	elif 2 in l1:
		print(1)
	else:
		print(-1)


for i in range(int(input())):
	N = int(input())
	li = list(map(int, input().split()))
	if sum(li) % 2 == 0:
		print(0)
	elif 2 in set(li):
		print(1)
	else:
		print(-1)


try:
	for _ in range(int(input())):
		n = int(input())
		f = 0
		a = list(map(int, input().split()))
		if sum(a) % 2 == 0:
			print(0)
		elif a.count(2) > 0:
			print(1)
		else:
			print(-1)
except:
	pass


import sys

def get_inpt():
	return sys.stdin.readline().strip()

def get_int():
	return int(sys.stdin.readline().strip())

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def p_str(x):
	sys.stdout.write(x + '\n')

def p_int(x):
	sys.stdout.write(str(x) + '\n')

def p_arr(arr):
	sys.stdout.write(' '.join(map(str, arr)) + '\n')
for _ in range(get_int()):
	n = get_int()
	arr = get_array()
	freq = {'even': 0, 'odd': 0, 2: 0, 3: 0}
	for i in range(n):
		if arr[i] % 2 == 0:
			freq['even'] += 1
			if arr[i] == 2:
				freq[2] += 1
		else:
			freq['odd'] += 1
			if arr[i] == 3:
				freq[3] += 1
	if freq['even'] % 2 == 0 and freq['odd'] % 2 == 0:
		print(0)
	elif freq['even'] % 2 != 0 and freq['odd'] % 2 == 0:
		print(0)
	elif freq['even'] % 2 == 0 and freq['odd'] % 2 != 0:
		if freq[2] >= 1:
			print(1)
		else:
			print(-1)
	elif freq[2] >= 1:
		print(1)
	else:
		print(-1)


def mksmevn():
	N = int(input())
	nums = list(map(int, input().split()))
	if sum(nums) % 2 == 0:
		print(0)
	elif 2 in nums:
		print(1)
	else:
		print(-1)
T = int(input())
for i in range(T):
	mksmevn()


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	sum_ = sum(arr)
	if sum_ & 1 == 0:
		print(0)
	elif sum_ & 1 != 0 and 2 in arr:
		print(1)
	else:
		print(-1)


try:
	T = int(input())
	for t in range(T):
		N = int(input())
		inputs = list(map(int, input().split()))
		if sum(inputs) % 2 == 0:
			print(0)
		elif 2 in inputs:
			print(1)
		else:
			print(-1)
except:
	pass


i = int(input())
while i > 0:
	n = int(input())
	array = list(map(int, input().split()))
	if sum(array) % 2 == 0:
		print(0)
	elif 2 in array:
		print(1)
	else:
		print(-1)
	i = i - 1


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	a = 0
	for i in l:
		a += i
	if a % 2 == 0:
		print(0)
	else:
		for i in l:
			if i == 2:
				print('1')
				break
		else:
			print('-1')


t = int(input())
for _ in range(0, t):
	n = int(input())
	list_n = list(map(int, input().split()))
	sum_list = sum(list_n)
	if sum_list % 2 == 0:
		print(0)
	elif sum_list % 2 != 0 and 2 in list_n:
		print(1)
	else:
		print(-1)


for _ in range(int(input())):
	N = int(input())
	arr = list(map(int, input().split()))
	sum1 = sum(arr)
	flag = 0
	if sum1 % 2 == 0:
		print(0)
		continue
	for i in arr:
		if i == 2:
			print(1)
			flag = 1
			break
	if flag:
		continue
	print(-1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	v = 0
	k2 = 0
	for i in a:
		if i % 2 == 1:
			v += 1
		if i == 2:
			k2 = 1
	if v % 2 == 0:
		print(0)
	elif v < n:
		if k2 == 1:
			print(1)
		else:
			print(-1)
	else:
		print(-1)