#K47757. Contiguous Subarray Partitioning
Contiguous Subarray Partitioning
Contiguous Subarray Partitioning
You are given a sequence of (N) integers and an integer (K). Your task is to determine if it is possible to partition the sequence into exactly (K) non-empty contiguous subarrays such that the sum of the maximum and minimum element in each subarray is the same. In other words, if a subarray is denoted by (A_i) (formed by a contiguous block of the original array), and if (\max(A_i)) and (\min(A_i)) are its maximum and minimum elements respectively, then you require
[
\max(A_i) + \min(A_i) = S \quad \text{for all } 1 \le i \le K,
]
for some constant (S).
Note: The intended solution (and the provided sample solution) simplifies the problem by checking a necessary condition: if (K = N) then the answer is always YES; otherwise, if the number of distinct elements in the sequence is less than or equal to (K), the answer is YES, and NO otherwise.
inputFormat
The input begins with an integer (T) representing the number of test cases. Each test case consists of two lines:
- The first line contains two integers (N) and (K), where (N) is the length of the sequence and (K) is the desired number of contiguous subarrays.
- The second line contains (N) space-separated integers representing the sequence.
outputFormat
For each test case, output a single line with either "YES" or "NO". Output "YES" if it is possible to partition the sequence under the given conditions; otherwise, output "NO".## sample
3
5 2
1 3 6 3 1
4 2
4 1 1 4
3 3
5 5 5
NO
YES
YES
</p>