#C8687. Partition Array into Equal Sum Subarrays
Partition Array into Equal Sum Subarrays
Partition Array into Equal Sum Subarrays
You are given an array of integers and a number k. Your task is to determine whether it is possible to partition the array into k contiguous subarrays such that the sum of each subarray is equal.
Let \( S \) be the total sum of the array. Only when \( S \) is divisible by \( k \) (i.e. \( S \bmod k = 0 \)) is it possible that each subarray has an equal sum. In that case, the target sum for each subarray is \( \frac{S}{k} \). Traverse the array, adding up the elements, and every time the current sum becomes equal to the target, reset it and count one valid subarray. If you can form exactly k subarrays, output "YES", otherwise output "NO".
Note: The subarrays must be contiguous and you are not allowed to rearrange the elements.
inputFormat
The first line contains a single integer t (1 ≤ t ≤ 10), representing the number of test cases.
For each test case, the first line contains two integers n and k (1 ≤ n ≤ 10^5, 1 ≤ k ≤ n), where n is the number of elements in the array and k is the number of subarrays to form.
The next line contains n integers representing the elements of the array. The numbers can be negative, zero, or positive.
outputFormat
For each test case, output one line containing either "YES" if it is possible to partition the array into k subarrays with equal sums, or "NO" if it is not possible.
The answers for different test cases should be printed on separate lines.
## sample3
4 2
1 2 2 1
5 3
3 3 3 3 3
6 3
2 2 2 2 2 2
YES
NO
YES
</p>