#C5047. Block Sequence Encoding
Block Sequence Encoding
Block Sequence Encoding
You are given a sequence of N integers and an integer K. Your task is to divide the sequence into K contiguous blocks of equal length and compute the sum of the elements in each block.
If it is not possible to divide the sequence into K blocks because N is not divisible by K, then you should output an empty list denoted by []
.
Mathematically, if the sequence is represented as \(a_1, a_2, \dots, a_N\), and if \(N \mod K = 0\), then each block will have a length of \(\frac{N}{K}\), and the output will be a list of sums where the i-th block sum is given by:
[ S_i = \sum_{j=(i-1)\cdot\frac{N}{K}+1}^{i\cdot\frac{N}{K}} a_j, \quad \text{for } i = 1, 2, \dots, K. ]
For example, given N = 6, K = 2 and the sequence [10, 20, 30, 40, 50, 60]
, the two blocks will be [10,20,30]
and [40,50,60]
with sums 60
and 150
respectively.
inputFormat
The input consists of two lines:
- The first line contains two integers N and K, where N is the length of the sequence and K is the number of blocks.
- The second line contains N space-separated integers representing the sequence.
outputFormat
If the sequence can be divided into K blocks of equal length, output the sum of each block separated by a space. Otherwise, output []
.
6 2
10 20 30 40 50 60
60 150