#K64602. Neighbor Sum Transformation
Neighbor Sum Transformation
Neighbor Sum Transformation
Given a list of integers, transform the list such that each element is replaced by the sum of its neighbors and itself. In other words, if the list is (a_0, a_1, \ldots, a_{n-1}), then the transformed list (s_0, s_1, \ldots, s_{n-1}) is defined as follows:
- (s_0 = a_0 + a_1) (for the first element),
- (s_{n-1} = a_{n-2} + a_{n-1}) (for the last element), and
- For (1 \le i \le n-2), (s_i = a_{i-1} + a_i + a_{i+1}).
If the input list is empty, return an empty list, and if it contains only one element, output the same element. This problem tests your ability to handle edge cases as well as perform simple iterative transformations.
inputFormat
The input is provided via standard input (stdin). The first line contains an integer (n) indicating the number of elements in the list. If (n > 0), the second line contains (n) space-separated integers representing the list.
outputFormat
Output the transformed list as a single line of (n) space-separated integers. For an empty list (i.e. when (n = 0)), output an empty line.## sample
0