Array Element Adjacent Sum-Python - Spicy Coders

Recent

Wednesday, July 26, 2017

Array Element Adjacent Sum-Python

Array Element Adjacent Sum Given an Array of length N, the program must print the sum of adjacent numbers of elements present in the array.

Input Format:
First line contains N. Second line contains N element values seperated by a space.

Output Format:
Adjacent sum of N elements seperated by a space.

Boundary Conditions: 2 <= N <= 100

Example Input/Output 1:
Input:
5
1 1 1 1 1

Output:
1 2 2 2 1

Example Input/Output 2:
Input:
10 
7 9 3 7 9 1 10 2 9 7

Output:
9 10 16 12 8 19 3 19 9 9

Example Input/Output 3:
Input: 
2
1 2

Output:
2 1

Source code:

m=int(input())
n=[int(i) for i in input().split()]
print(n[1],' ',*[n[i-1]+n[i+1] for i in range(1,m-1)],' ',n[m-2]) 

No comments:

Post a Comment