Sum of the Array

Problem Statement

Given an array of integers, print a sum triangle from it such that the first level has all array elements. From then on, at each level, the number of elements is one less than the previous level, and the elements at the level are the sum of consecutive two elements in the previous level.

Input Format:

  • The first line contains an integer N, denoting the size of the array.
  • The second line contains N space-separated integers denoting the elements of the array.

Output Format:

  • Print the numbers at each level in reverse order.

Constraints:

  • 1 ≤ N ≤ 100
  • -105 ≤ A[i] ≤ 105
Sample Input
Sample Output

Solution

file_type_python sum_of_the_array.py
n = int(input())
l = list(map(int, input().split()))
res = [[0]*n for _ in range(n)]
for i in range(n):
    res[n - 1][i] = l[i]
for i in range(n-2, -1, -1):
    for j in range(i + 1):
        res[i][j] = res[i + 1][j] + res[i + 1][j + 1]
for r in res:
    print(*r[:len(r) - r.count(0)])

Comments

Load Comments