#B4147. High-Dimensional Array Axis Sum
High-Dimensional Array Axis Sum
High-Dimensional Array Axis Sum
You are given a high-dimensional array a
with n dimensions. The size of the array is given by \(d_0 \times d_1 \times \cdots \times d_{n-1}\). Each dimension is referred to as an axis, where \(d_i\) corresponds to the \(i\)-th axis (with indices starting from 0).
The sum of the elements along the \(x\)-th axis is defined as an \((n-1)\)-dimensional array \(S\) of size \[ d_0 \times d_1 \times \cdots \times d_{x-1} \times d_{x+1} \times \cdots \times d_{n-1} \] with the following formula: \[ S[w_0][w_1]\cdots[w_{x-1}][w_{x+1}]\cdots[w_{n-1}] = \sum_{i=0}^{d_x-1} a[w_0][w_1]\cdots[w_{x-1}][i][w_{x+1}]\cdots[w_{n-1}] \]
Your task is to compute the summation along the specified \(x\)-th axis and output the resulting \((n-1)\)-dimensional array in row-major order (flattened into a single line of space separated values).
inputFormat
The first line contains two integers: n
(the number of dimensions) and x
(the axis along which to sum, where \(0 \le x < n\)).
The second line contains n
integers: \(d_0, d_1, \ldots, d_{n-1}\) representing the size in each dimension.
The third line contains \(d_0 \times d_1 \times \cdots \times d_{n-1}\) integers representing the elements of the high-dimensional array in row-major order.
outputFormat
Output a single line containing \(\frac{d_0 \times d_1 \times \cdots \times d_{n-1}}{d_x}\) integers, the elements of the resulting array after summing along the \(x\)-th axis, in row-major order (space separated).
sample
2 1
2 3
1 2 3 4 5 6
5 7
</p>