#K51247. Minimum Absolute Difference Between Subarray Sums

    ID: 29045 Type: Default 1000ms 256MiB

Minimum Absolute Difference Between Subarray Sums

Minimum Absolute Difference Between Subarray Sums

You are given an array of n integers and an integer k. Your task is to compute the minimum absolute difference between the sums of any two distinct contiguous subarrays of length k. In other words, if we define the sum of a subarray starting at index i as

\(S_i = \sum_{j=i}^{i+k-1} a_j\)

for every valid index i, you need to find the minimum value of \(|S_i - S_j|\) for any two different indices i and j.

Note: The subarrays considered must be contiguous segments of the array and must be of length exactly k.

inputFormat

The input is read from standard input (stdin) and has the following format:

  • The first line contains two space-separated integers n and k, where n is the number of elements in the array and k is the length of the subarray.
  • The second line contains n space-separated integers representing the elements of the array.

outputFormat

Print a single integer which is the minimum absolute difference between the sums of any two contiguous subarrays of length k. The output should be written to standard output (stdout).

## sample
5 2
10 -2 4 -1 2
1