


You can assume that the value of ‘K’ will always be less than or equal to ‘N’. So, the answer will always exist.
The first line of input contains two single space-separated integers ‘N’ and ‘K’ representing the size of the array and the given integer, respectively.
The second line of input contains 'N' single space-separated integers representing the array elements.
Print the minimum possible subarray sum.
Print the output of each test case in a new line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= N <= 10^5
1 <= K <= N
-10^5 <= ARR[i] <= 10^5
Time Limit: 1sec
We have a simple brute force solution for this problem. We will traverse over all subarrays of size ‘K’ and calculate their sum. Finally, we return the minimum of all subarray sums.
We can naively find all subarrays of size ‘K’ by two nested loops. The outer loop (running from 0 to N - K) will iterate over the starting index of all the subarrays, and the inner loop (running from i to i + K) will be used to calculate the sum of elements of the current subarray of size ‘K’.
A sliding window of size ‘K’ can be maintained while traversing the array left to right. Using this, we can easily compute the sum of all subarrays (other than the first subarray) of size ‘K’ by removing the first element of the previous window/subarray and adding the current element to the window.
Algorithm