Odd Sum Segments (Codeforces 1196B)
Tags: Math, constructive algorithms

You are given an array $$$a$$$ consisting of $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$. You want to split it into exactly $$$k$$$ non-empty non-intersecting subsegments such that each subsegment has odd sum (i. e. for each subsegment, the sum of all elements that belong to this subsegment is odd). It is impossible to rearrange (shuffle) the elements of a given array. Each of the $$$n$$$ elements of the array $$$a$$$ must belong to exactly one of the $$$k$$$ subsegments.Let's see some examples of dividing the array of length $$$5$$$ into $$$3$$$ subsegments (not necessarily with odd sums): $$$[1, 2, 3, 4, 5]$$$ is the initial array, then all possible ways to divide it into $$$3$$$ non-empty non-intersecting subsegments are described below:  $$$[1], [2], [3, 4, 5]$$$;  $$$[1], [2, 3], [4, 5]$$$;  $$$[1], [2, 3, 4], [5]$$$;  $$$[1, 2], [3], [4, 5]$$$;  $$$[1, 2], [3, 4], [5]$$$;  $$$[1, 2, 3], [4], [5]$$$. Of course, it can be impossible to divide the initial array into exactly $$$k$$$ subsegments in such a way that each of them will have odd sum of elements. In this case print "NO". Otherwise, print "YES" and any possible division of the array. See the output format for the detailed explanation.You have to answer $$$q$$$ independent queries.

Input: The first line contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. Then $$$q$$$ queries follow.The first line of the query contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2 \cdot 10^5$$$) — the number of elements in the array and the number of subsegments, respectively.The second line of the query contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).

Output: For each query, print the answer to it. If it is impossible to divide the initial array into exactly $$$k$$$ subsegments in such a way that each of them will have odd sum of elements, print "NO" in the first line. Otherwise, print "YES" in the first line and any possible division of the array in the second line. The division can be represented as $$$k$$$ integers $$$r_1$$$, $$$r_2$$$, ..., $$$r_k$$$ such that $$$1 \le r_1 < r_2 < \dots < r_k = n$$$, where $$$r_j$$$ is the right border of the $$$j$$$-th segment (the index of the last element that belongs to the $$$j$$$-th segment), so the array is divided into subsegments $$$[1; r_1], [r_1 + 1; r_2], [r_2 + 1, r_3], \dots, [r_{k - 1} + 1, n]$$$. Note that $$$r_k$$$ is always $$$n$$$ but you should print it anyway. 


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

Sample Outputs:
YES
1 3 5
NO
NO

Solution (Python):
import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

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

def LLI():
    return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
    return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
    return [float(x) for x in sys.stdin.readline().split()]

def LS():
    return sys.stdin.readline().split()

def I():
    return int(sys.stdin.readline())

def F():
    return float(sys.stdin.readline())

def S():
    return input()

def pf(s):
    return print(s, flush=True)

def pe(s):
    return print(str(s), file=sys.stderr)

def JA(a, sep):
    return sep.join(map(str, a))

def JAA(a, s, t):
    return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
    return t if c else f

def YES(c):
    return IF(c, 'YES', 'NO')

def Yes(c):
    return IF(c, 'Yes', 'No')

def main():
    t = I()
    rr = []
    for _ in range(t):
        (n, k) = LI()
        a = LI()
        o = [(i, c) for (i, c) in enumerate(a, 1) if c % 2 == 1]
        if len(o) < k or (len(o) - k) % 2 > 0:
            rr.append('NO')
        else:
            rr.append('YES')
            if k == 1:
                rr.append(n)
            else:
                rr.append(JA(map(lambda x: x[0], o[:k - 1] + [(n, 0)]), ' '))
    return JA(rr, '\n')
print(main())

Solution (Python):
from sys import stdin

def arr_inp(n):
    if n == 1:
        return [int(x) for x in stdin.readline().split()]
    elif n == 2:
        return [float(x) for x in stdin.readline().split()]
    else:
        return list(stdin.readline()[:-1])
ans = []
for i in range(int(input())):
    (n, k) = arr_inp(1)
    a = arr_inp(1)
    odds = len(list(filter(lambda x: x & 1, a)))
    if odds < k or (odds - k) % 2:
        ans.append('NO')
    else:
        ans.append('YES')
        (c, s) = (1, '')
        for j in range(n):
            if c == k:
                break
            if a[j] & 1:
                c += 1
                s += str(j + 1) + ' '
        s += str(n)
        ans.append(s)
print('\n'.join(ans))

Solution (Python):
import atexit
import io
import sys
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER

@atexit.register
def write():
    sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
t = int(input())
for i in range(t):
    (n, k) = list(map(int, input().split()))
    v = list(map(int, input().split()))
    cont = 0
    impares = 0
    ans = []
    for i in range(len(v)):
        if v[i] % 2 != 0:
            impares += 1
            ans.append(i + 1)
    if impares < k:
        print('NO')
    else:
        impares -= k
        if impares % 2 != 0:
            print('NO')
        else:
            print('YES')
            for i in range(k - 1):
                print(f'{ans[i]}', end=' ')
            print(f'{n}')