Similar Pairs (Codeforces 1360C)
Tags: Greedy, graph matchings

We call two numbers $$$x$$$ and $$$y$$$ similar if they have the same parity (the same remainder when divided by $$$2$$$), or if $$$|x-y|=1$$$. For example, in each of the pairs $$$(2, 6)$$$, $$$(4, 3)$$$, $$$(11, 7)$$$, the numbers are similar to each other, and in the pairs $$$(1, 4)$$$, $$$(3, 12)$$$, they are not.You are given an array $$$a$$$ of $$$n$$$ ($$$n$$$ is even) positive integers. Check if there is such a partition of the array into pairs that each element of the array belongs to exactly one pair and the numbers in each pair are similar to each other.For example, for the array $$$a = [11, 14, 16, 12]$$$, there is a partition into pairs $$$(11, 12)$$$ and $$$(14, 16)$$$. The numbers in the first pair are similar because they differ by one, and in the second pair because they are both even.

Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow.Each test case consists of two lines.The first line contains an even positive integer $$$n$$$ ($$$2 \le n \le 50$$$) — length of array $$$a$$$.The second line contains $$$n$$$ positive integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$).

Output: For each test case print:  YES if the such a partition exists,  NO otherwise. The letters in the words YES and NO can be displayed in any case.

Note: The first test case was explained in the statement.In the second test case, the two given numbers are not similar.In the third test case, any partition is suitable.

Sample Inputs:
7
4
11 14 16 12
2
1 8
4
1 1 1 1
4
1 2 5 6
2
12 13
6
1 6 3 10 5 8
6
1 12 3 10 5 8

Sample Outputs:
YES
NO
YES
YES
YES
YES
NO

Solution (Python):
import math, bisect
from collections import defaultdict

def find_bit(a, b):
    xor = a ^ b
    count = 0
    while xor:
        count += xor & 1
        xor >>= 1
    return count

def main():
    t = int(input())
    narr = []
    sarr = []
    for _ in range(t):
        n = int(input())
        narr.append(n)
        arr = list(map(int, input().strip().split()))[:n]
        sarr.append(arr)
    for j in range(t):
        n = narr[j]
        arr = sarr[j]
        arr.sort()
        ecount = ocount = 0
        for a in arr:
            ocount += a % 2
            ecount += not a % 2
        if not ocount % 2 and (not ecount % 2):
            print('YES')
        else:
            flag = False
            for i in range(n - 1):
                if abs(arr[i] - arr[i + 1]) == 1:
                    flag = True
                    break
            if flag:
                print('YES')
            else:
                print('NO')
if __name__ == '__main__':
    main()

Solution (Python):
for l in [*open(0)][2::2]:
    b = c = 0
    p = -1
    for i in sorted(map(int, l.split())):
        b += i - p == 1
        c += i % 2
        p = i
    print('NYOE S'[b > 0 or not c % 2::2])

Solution (Python):
for l in [*open(0)][2::2]:
    b = c = 0
    p = -1
    for i in sorted(map(int, l.split())):
        b += i - p == 1
        c += i % 2
        p = i
    print('NYOE S'[b > 0 or not c % 2::2])