Choose the Different Ones! (Codeforces 1927C)
Tags: Greedy, brute force

Given an array $$$a$$$ of $$$n$$$ integers, an array $$$b$$$ of $$$m$$$ integers, and an even number $$$k$$$.Your task is to determine whether it is possible to choose exactly $$$\frac{k}{2}$$$ elements from both arrays in such a way that among the chosen elements, every integer from $$$1$$$ to $$$k$$$ is included.For example:  If $$$a=[2, 3, 8, 5, 6, 5]$$$, $$$b=[1, 3, 4, 10, 5]$$$, $$$k=6$$$, then it is possible to choose elements with values $$$2, 3, 6$$$ from array $$$a$$$ and elements with values $$$1, 4, 5$$$ from array $$$b$$$. In this case, all numbers from $$$1$$$ to $$$k=6$$$ will be included among the chosen elements. If $$$a=[2, 3, 4, 5, 6, 5]$$$, $$$b=[1, 3, 8, 10, 3]$$$, $$$k=6$$$, then it is not possible to choose elements in the required way. Note that you are not required to find a way to choose the elements — your program should only check whether it is possible to choose the elements in the required way.

Input: The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The descriptions of the test cases follow.The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 \le n, m \le 2\cdot10^5$$$, $$$2 \le k \le 2 \cdot \min(n, m)$$$, $$$k$$$ is even) — the length of array $$$a$$$, the length of array $$$b$$$, and the number of elements to be chosen, respectively.The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the elements of array $$$a$$$.The third line of each test case contains $$$m$$$ integers $$$b_1, b_2, \dots, b_m$$$ ($$$1 \le b_j \le 10^6$$$) — the elements of array $$$b$$$.It is guaranteed that the sum of values $$$n$$$ and $$$m$$$ over all test cases in a test does not exceed $$$4 \cdot 10^5$$$.

Output: Output $$$t$$$ lines, each of which is the answer to the corresponding test case. As the answer, output "YES" if it is possible to choose $$$\frac{k}{2}$$$ numbers from each array in such a way that among the chosen elements, every integer from $$$1$$$ to $$$k$$$ is included. Otherwise, output "NO".You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.

Note: In the first test case of the example, it is possible to choose elements equal to $$$2$$$, $$$3$$$, and $$$6$$$ from array $$$a$$$ and elements equal to $$$1$$$, $$$4$$$, and $$$5$$$ from array $$$b$$$. Thus, all numbers from $$$1$$$ to $$$k=6$$$ are included among the chosen elements.In the second test case of the example, it can be shown that it is not possible to choose exactly three elements from each array in the required way.In the third test case of the example, it is possible to choose elements equal to $$$1$$$ and $$$3$$$ from array $$$a$$$ and elements equal to $$$2$$$ and $$$4$$$ from array $$$b$$$. Thus, all numbers from $$$1$$$ to $$$k=4$$$ are included among the chosen elements.

Sample Inputs:
6
6 5 6
2 3 8 5 6 5
1 3 4 10 5
6 5 6
2 3 4 5 6 5
1 3 8 10 3
3 3 4
1 3 5
2 4 6
2 5 4
1 4
7 3 4 4 2
1 4 2
2
6 4 4 2
1 5 2
3
2 2 1 4 3

Sample Outputs:
YES
NO
YES
YES
NO
NO

Solution (Python):
def solve():
    (n, m, k) = map(int, input().split())
    first = list(map(int, input().split()))
    second = list(map(int, input().split()))
    counts = [0] * (k + 1)
    for num in first:
        if num <= k:
            counts[num] |= 1
    for num in second:
        if num <= k:
            counts[num] |= 2
    c = [0] * 4
    for num in counts:
        c[num] += 1
    if c[1] > k // 2 or c[2] > k // 2 or c[1] + c[2] + c[3] < k:
        print('NO')
    else:
        print('YES')

def main():
    tc = int(input())
    for _ in range(tc):
        solve()
main()

Solution (Python):
def in_range(a, b, k):
    count = [0] * (k + 1)
    for i in a:
        if i <= k:
            count[i] |= 1
    for i in b:
        if i <= k:
            count[i] |= 2
    result = [0] * 4
    for i in count:
        result[i] += 1
    if result[1] > k // 2 or result[2] > k // 2 or result[1] + result[2] + result[3] != k:
        return False
    return True

def main() -> None:
    t = int(input())
    for _ in range(t):
        (n, m, k) = map(int, input().split())
        a = list(map(int, input().split()))
        b = list(map(int, input().split()))
        print('YES' if in_range(a, b, k) else 'NO')
if __name__ == '__main__':
    main()

Solution (Python):
def solution():
    (a, b, k) = map(int, input().split())
    seta = set(map(int, input().split()))
    setb = set(map(int, input().split()))
    count1 = 0
    count2 = 0
    both = 0
    for i in range(1, k + 1):
        if i in seta and i in setb:
            both += 1
        elif i in seta:
            count1 += 1
        elif i in setb:
            count2 += 1
        else:
            print('NO')
            return
    if count1 <= k / 2 and count2 <= k / 2:
        print('YES')
    else:
        print('NO')
n = int(input())
for i in range(n):
    solution()