#C4336. Replace with Sum of Neighbors
Replace with Sum of Neighbors
Replace with Sum of Neighbors
You are given an array representing values at houses. For each test case, replace each value with the sum of its neighbors. For the first house, use only its right neighbor and for the last house, use only its left neighbor. If there is only one house, output 0.
More formally, let \(A\) be the array of length \(N\). Then:
- If \(N = 1\), the answer is \([0]\).
- If \(N > 1\), then the new array \(B\) is defined as: \[ B_0 = A_1, \quad B_{N-1} = A_{N-2}, \quad \text{and for } 1 \le i \le N-2, \quad B_i = A_{i-1} + A_{i+1}. \]
Process \(T\) test cases accordingly.
inputFormat
The input starts with an integer \(T\) representing the number of test cases. Each test case consists of:
- An integer \(N\) denoting the number of houses.
- A line of \(N\) space-separated integers representing the values at the houses.
outputFormat
For each test case, output a single line containing the transformed array where each element is replaced by the sum of its neighbors. The elements should be separated by a space.
## sample4
1
7
2
5 6
3
1 2 3
4
5 6 7 8
0
6 5
2 4 2
6 12 14 7
</p>