#C8699. Sum of Contiguous Subarrays

    ID: 52709 Type: Default 1000ms 256MiB

Sum of Contiguous Subarrays

Sum of Contiguous Subarrays

You are given an array of integers and an integer (k). Your task is to compute the sum of every contiguous subarray of length (k). More formally, if the array is (a_1, a_2, \dots, a_n), then for every (i) such that (1 \leq i \leq n-k+1) you should compute:

[ S_i = \sum_{j=i}^{i+k-1} a_j ]

If (k) is greater than (n) (the number of elements in the array), output an empty result. The result should be printed as space-separated integers in a single line.

Example: For the array [1, 2, 3, 4, 5] and (k = 3), the result is [6, 9, 12] because:

  • (6 = 1+2+3)
  • (9 = 2+3+4)
  • (12 = 3+4+5)

inputFormat

The input is read from standard input (stdin) and consists of three lines:

  1. The first line contains an integer (n) representing the number of elements in the array.
  2. The second line contains (n) space-separated integers representing the elements of the array.
  3. The third line contains an integer (k), the length of the subarrays.

outputFormat

Print the computed sums of all contiguous subarrays of length (k) as space-separated integers on a single line to standard output (stdout). If no such subarray exists, print an empty line.## sample

5
1 2 3 4 5
3
6 9 12