Sum of Adjacent Elements
EasyPractice library · +10 XPtwo pointersarraysloops
Given an array of N integers, compute the sum of each adjacent pair of elements. Print these sums in the order they appear. For an array of size N, there will be N-1 such sums.
Input
The first line contains an integer N (2 <= N <= 1000). The second line contains N space-separated integers a_1, a_2, ..., a_N.
Output
Print N-1 space-separated integers, where the i-th integer is the sum of a_i and a_{i+1}.
Constraints
2 <= N <= 1000. Each integer a_i is between -1000 and 1000.
Time limit 1 s (native code; slower languages get more)Memory 256 MB12 hidden tests
Example 1
Input
5 1 2 3 4 5
Output
3 5 7 9
Sums are (1+2), (2+3), (3+4), (4+5).
Example 2
Input
3 10 -5 20
Output
5 15
Sums are (10 + -5) and (-5 + 20).
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
5 1 2 3 4 5
Example 2 input
3 10 -5 20