K-Subarray Sum

Easy

Given an array and a number k, find the sum of all the subarrays of size k.

Example
Array: [3, 5, 6, 2, 4, 7, 8]
k: 3
Here, the subarrays of size k and their sum are:
[3 5 6] => 14
[5 6 2] => 13
[6 2 4] => 12
[2 4 7] => 13
[4 7 8] => 19
Answer: [14, 13, 12, 13, 19]

Testing

Input Format

The first line contains an integer ‘T’ denoting the number of test cases.

For each test case, the input contains two lines:

  • An integer 'n' denoting the size of the array A and 'k' denoting the size of the subarray.
  • n space-separated integers denoting elements of the array A.

Output format

For each test-cases, the output has n - k + 1 number denoting the elements of the resultant array.

Sample Input

2
7 3
3 5 6 2 4 7 8
6 1
1 3 3 3 4 4

Expected Output

14 13 12 13 19
1 3 3 3 4 4

Constraints

1 <= T <= 100

1 <= n <= 104

1 <= k <= n

1 <= Ai <= 104

Editorial Link: Editorial