B. Cost of the Array
brute force; constructive algorithms; greedy; math
You are given an array \(a\) of length \(n\) and an even integer \(k\) (\(2 \le k \le n\)). You need to split the array \(a\) into exactly \(k\) non-empty subarrays\(^{\dagger}\) such that each element of the array \(a\) belongs to exactly one subarray.Next, all subarrays with even indices (second, fourth, \(\ldots\), \(k\)-th) are concatenated into a single array \(b\). After that, \(0\) is added to the end of the array \(b\).The cost of the array \(b\) is defined as the minimum index \(i\) such that \(b_i \neq i\). For example, the cost of the array \(b = [1, 2, 4, 5, 0]\) is \(3\), since \(b_1 = 1\), \(b_2 = 2\), and \(b_3 \neq 3\). Determine the minimum cost of the array \(b\) that can be obtained with an optimal partitioning of the array \(a\) into subarrays.\(^{\dagger}\)An array \(x\) is a subarray of an array \(y\) if \(x\) can be obtained from \(y\) by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(2 \le k \le n \le 2 \cdot 10^5\), \(k\) is even) — the length of the array \(a\) and the number of subarrays.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) — the elements of the array \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
For each test case, output a single integer — the minimum cost of the array \(b\) that can be obtained.
In the first test case, there are only two possible partitionings: \([[1], [1, 1]]\) and \([[1, 1], [1]]\). In either case, \(b_1 = 1\), and \(b_2 \ne 2\), so the cost is \(2\).In the second test case, there is only one possible partitioning, where \(b = [1, 2, 3, 4, 0]\), so the cost is \(5\) (\(b_5 = 0 \ne 5\)).In the third test case, the following partitioning works: \([[1], [1, 1], [2], [2]]\). Then \(b = [1, 1, 2, 0]\), and the cost is \(2\).