Odd/Even Increments (Codeforces 1669C)
Tags: Greedy, implementation

Given an array $$$a=[a_1,a_2,\dots,a_n]$$$ of $$$n$$$ positive integers, you can do operations of two types on it:  Add $$$1$$$ to every element with an odd index. In other words change the array as follows: $$$a_1 := a_1 +1, a_3 := a_3 + 1, a_5 := a_5+1, \dots$$$.  Add $$$1$$$ to every element with an even index. In other words change the array as follows: $$$a_2 := a_2 +1, a_4 := a_4 + 1, a_6 := a_6+1, \dots$$$.Determine if after any number of operations it is possible to make the final array contain only even numbers or only odd numbers. In other words, determine if you can make all elements of the array have the same parity after any number of operations.Note that you can do operations of both types any number of times (even none). Operations of different types can be performed a different number of times.

Input: The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases.The first line of each test case contains an integer $$$n$$$ ($$$2 \leq n \leq 50$$$) — the length of the array.The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^3$$$) — the elements of the array.Note that after the performed operations the elements in the array can become greater than $$$10^3$$$.

Output: Output $$$t$$$ lines, each of which contains the answer to the corresponding test case. As an answer, output "YES" if after any number of operations it is possible to make the final array contain only even numbers or only odd numbers, and "NO" otherwise.You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).

Note: For the first test case, we can increment the elements with an even index, obtaining the array $$$[1, 3, 1]$$$, which contains only odd numbers, so the answer is "YES".For the second test case, we can show that after performing any number of operations we won't be able to make all elements have the same parity, so the answer is "NO".For the third test case, all elements already have the same parity so the answer is "YES".For the fourth test case, we can perform one operation and increase all elements at odd positions by $$$1$$$, thus obtaining the array $$$[1001, 1, 1001, 1, 1001]$$$, and all elements become odd so the answer is "YES".

Sample Inputs:
4
3
1 2 1
4
2 2 2 3
4
2 2 2 2
5
1000 1 1000 1 1000

Sample Outputs:
YES
NO
YES
YES

Solution (Python):
for _ in range(int(input())):
    n = int(input())
    l = list(map(int, input().split()))
    ls_1 = []
    ls_2 = []
    for i in range(1, n, 2):
        ls_1.append(l[i] + 1)
    for i in range(0, n, 2):
        ls_1.append(l[i])
    for i in range(1, n, 2):
        ls_2.append(l[i])
    for i in range(0, n, 2):
        ls_2.append(l[i] + 1)
    c_e = 0
    c_o = 0
    for i in l:
        if i % 2 == 0:
            c_e += 1
        else:
            c_o += 1
    if c_e == n or c_o == n:
        print('YES')
    else:
        c_e = 0
        c_o = 0
        c_e2 = c_o2 = 0
        for i in ls_1:
            if i % 2 == 0:
                c_e += 1
            else:
                c_o += 1
        for i in ls_2:
            if i % 2 == 0:
                c_e2 += 1
            else:
                c_o2 += 1
        if c_o2 == n or c_e2 == n or c_e == n or (c_o == n):
            print('YES')
        else:
            print('NO')

Solution (Python):
from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip

def same_type(arr):
    types = [num % 2 for num in arr]
    l = len(arr)
    if types == [0] * l or types == [1] * l:
        return True
    return False

def main():
    t = int(input())
    for _ in range(t):
        n = int(input())
        a = list(map(int, input().split()))
        (evens, odds) = ([], [])
        for i in range(n):
            if i % 2 == 0:
                evens.append(a[i])
            else:
                odds.append(a[i])
        if same_type(evens) and same_type(odds):
            print('YES')
        else:
            print('NO')
BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = 'x' in file.mode or 'r' not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b'\n') + (not b)
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            (self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode('ascii'))
        self.read = lambda : self.buffer.read().decode('ascii')
        self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
    """"""
    (sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop('end', '\n'))
    if kwargs.pop('flush', False):
        file.flush()
if sys.version_info[0] < 3:
    (sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
    (sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
if __name__ == '__main__':
    main()

Solution (Python):
t = int(input())
tc = []
for _ in range(t):
    n = int(input())
    even = []
    odd = []
    arr = list(map(int, input().strip().split()))
    res = all((arr[idx] % 2 == 0 for idx in range(0, len(arr))))
    res_ = all((arr[idx] % 2 == 1 for idx in range(0, len(arr))))
    if res or res_:
        print('YES')
    else:
        for i in range(n):
            if i % 2 == 0:
                even.append(arr[i])
            else:
                odd.append(arr[i])
        res = all((even[idx] % 2 == 0 for idx in range(0, len(even))))
        res_ = all((odd[idx] % 2 == 1 for idx in range(0, len(odd))))
        res1 = all((even[idx] % 2 == 1 for idx in range(0, len(even))))
        res1_ = all((odd[idx] % 2 == 0 for idx in range(0, len(odd))))
        if res and res_ or (res1 and res1_):
            print('YES')
        else:
            print('NO')