#C7876. Min Difference Partition
Min Difference Partition
Min Difference Partition
Given an array of integers and an integer \(K\), partition the array into two contiguous subarrays such that one subarray contains exactly \(K\) consecutive elements. The task is to minimize the absolute difference between the sum of the elements in the chosen subarray and the sum of the remaining elements. If \(K\) is less than 1 or greater than the number of elements in the array, the result is considered to be \(\infty\) (infinity).
For example, consider the array [1, 2, 3, 4, 5] and \(K = 2\). You can choose a contiguous subarray of 2 elements anywhere in the array. The optimal partition in this case results in a minimum difference of 1.
The solution should be implemented to read input from standard input and write the result to standard output. The literal infinity should be output as "inf" in cases where the partitioning is invalid.
inputFormat
The first line contains an integer \(T\), 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 number of elements in the array.
- The second line contains \(N\) integers separated by spaces. If \(N = 0\), the second line will be empty.
outputFormat
For each test case, output a single line containing the minimum difference between the sum of the chosen contiguous subarray of length \(K\) and the sum of the remaining elements. If \(K\) is invalid (i.e., \(K N\)), output "inf".
## sample8
5 2
1 2 3 4 5
5 3
10 20 15 5 25
1 1
100
4 2
5 5 5 5
4 4
1 2 3 4
4 5
1 2 3 4
4 -1
1 2 3 4
0 1
1
5
100
0
10
inf
inf
inf
</p>