Given a array, write a program to construct a triangle where last row contains elements of given array, every element of second last row contains sum of below two elements and so on.
Example:
Input: arr[] = {4, 7, 3, 6, 7};
Output:
81
40 41
21 19 22
11 10 9 13
4 7 3 6 7
Input: {10, 40, 50}
Output:
140
50 90
10 40 50
An important observation about output is final value is at the top and top element needs to printed first. Therefore, we use a 2D auxiliary array to construct the triangle in bottom up manner and then print the triangle. An element tri[i][j] of 2D array can be calculated as sum of tri[i+1][j] and tri[i+1][j+1].
Below is the implementation of above idea :
C++
#include <bits/stdc++.h>
using namespace std;
void printTriangle( int arr[], int n)
{
int tri[n][n];
memset (tri, 0, sizeof (tri));
for ( int i = 0; i < n ; i++)
tri[n-1][i] = arr[i];
for ( int i = n-2; i >=0; i--)
for ( int j = 0; j <= i; j++)
tri[i][j] = tri[i+1][j] + tri[i+1][j+1];
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j <= i ; j++)
cout << tri[i][j]<< " " ;
cout << endl;
}
}
int main()
{
int arr[] = {4, 7, 3, 6, 7};
int n = sizeof (arr)/ sizeof (arr[0]);
printTriangle(arr, n);
return 0;
}
|
Java
class Test{
static int arr[] = new int []{ 4 , 7 , 3 , 6 , 7 };
public static void printTriangle( int n)
{
int tri[][] = new int [n][n];
for ( int i = 0 ; i < n ; i++)
tri[n- 1 ][i] = arr[i];
for ( int i = n- 2 ; i >= 0 ; i--)
for ( int j = 0 ; j <= i; j++)
tri[i][j] = tri[i+ 1 ][j] + tri[i+ 1 ][j+ 1 ];
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j <= i ; j++)
System.out.print(tri[i][j] + " " );
System.out.println();
}
}
public static void main(String[] args)
{
printTriangle(arr.length);
}
}
|
Python3
def printTriangle(arr, n):
tri = [[ 0 for i in range (n)]
for i in range (n)]
for i in range (n):
tri[n - 1 ][i] = arr[i]
i = n - 2
while (i > = 0 ):
for j in range ( 0 , i + 1 , 1 ):
tri[i][j] = (tri[i + 1 ][j] +
tri[i + 1 ][j + 1 ])
i - = 1
for i in range ( 0 , n, 1 ):
for j in range ( 0 , i + 1 , 1 ):
print (tri[i][j], end = " " )
print ( "\n" , end = "")
if __name__ = = '__main__' :
arr = [ 4 , 7 , 3 , 6 , 7 ]
n = len (arr)
printTriangle(arr, n)
|
C#
using System;
class GFG {
static int []arr = new int []{4, 7, 3, 6, 7};
public static void printTriangle( int n)
{
int [,]tri = new int [n, n];
for ( int i = 0; i < n ; i++)
tri[n - 1, i] = arr[i];
for ( int i = n - 2; i >= 0; i--)
for ( int j = 0; j <= i; j++)
tri[i, j] = tri[i + 1, j] +
tri[i + 1, j + 1];
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j <= i ; j++)
Console.Write(tri[i, j] + " " );
Console.WriteLine();
}
}
public static void Main()
{
printTriangle(arr.Length);
}
}
|
PHP
<?php
function printTriangle( $arr , $n )
{
$tri [ $n ][ $n ] = array ( array ());
array_fill (0, count ( $tri ), 0);
for ( $i = 0; $i < $n ; $i ++)
$tri [ $n - 1][ $i ] = $arr [ $i ];
for ( $i = $n - 2; $i >= 0; $i --)
for ( $j = 0; $j <= $i ; $j ++)
$tri [ $i ][ $j ] = $tri [ $i + 1][ $j ] +
$tri [ $i + 1][ $j + 1];
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j <= $i ; $j ++)
echo $tri [ $i ][ $j ] . " " ;
echo "\n" ;
}
}
$arr = array (4, 7, 3, 6, 7);
$n = count ( $arr );
printTriangle( $arr , $n );
?>
|
Javascript
<script>
function printTriangle(arr, n)
{
var tri = new Array(n).fill(0).map((item) => new Array(n).fill(0));
for ( var i = 0; i < n; i++) tri[n - 1][i] = arr[i];
for ( var i = n - 2; i >= 0; i--)
for ( var j = 0; j <= i; j++)
tri[i][j] = tri[i + 1][j] + tri[i + 1][j + 1];
for ( var i = 0; i < n; i++) {
for ( var j = 0; j <= i; j++)
document.write(tri[i][j] + " " );
document.write( "<br>" );
}
}
var arr = [4, 7, 3, 6, 7];
var n = arr.length;
printTriangle(arr, n);
</script>
|
Output:
81
40 41
21 19 22
11 10 9 13
4 7 3 6 7
Time Complexity: O(n2)
Auxiliary Space: O(n2) because using array “tr”
Thanks to nish for suggesting this solution.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above