Sum triangle from array
Given an array of integers, print a sum triangle from it such that the first level has all array elements. From then, at each level number of elements is one less than the previous level and elements at the level is be the Sum of consecutive two elements in the previous level.
Example :
Input : A = {1, 2, 3, 4, 5}
Output : [48]
[20, 28]
[8, 12, 16]
[3, 5, 7, 9]
[1, 2, 3, 4, 5]
Explanation :
Here, [48]
[20, 28] -->(20 + 28 = 48)
[8, 12, 16] -->(8 + 12 = 20, 12 + 16 = 28)
[3, 5, 7, 9] -->(3 + 5 = 8, 5 + 7 = 12, 7 + 9 = 16)
[1, 2, 3, 4, 5] -->(1 + 2 = 3, 2 + 3 = 5, 3 + 4 = 7, 4 + 5 = 9)
Approach :
- Recursion is the key. At each iteration create a new array which contains the Sum of consecutive elements in the array passes as parameter.
- Make a recursive call and pass the newly created array in the previous step.
- While back tracking print the array (for printing in reverse order).
Below is implementation of the above approach :C++
// C++ program to create Special triangle.#include<bits/stdc++.h>usingnamespacestd;// Function to generate Special TrianglevoidprintTriangle(intA[] ,intn){// Base caseif(n < 1)return;// Creating new array which contains the// Sum of consecutive elements in// the array passes as parameter.inttemp[n - 1];for(inti = 0; i < n - 1; i++){intx = A[i] + A[i + 1];temp[i] = x;}// Make a recursive call and pass// the newly created arrayprintTriangle(temp, n - 1);// Print current array in the end so// that smaller arrays are printed firstfor(inti = 0; i < n ; i++){if(i == n - 1)cout << A[i] <<" ";elsecout << A[i] <<", ";}cout << endl;}// Driver functionintmain(){intA[] = { 1, 2, 3, 4, 5 };intn =sizeof(A) /sizeof(A[0]);printTriangle(A, n);}// This code is contributed by Smitha Dinesh SemwalJava
// Java program to create Special triangle.importjava.util.*;importjava.lang.*;publicclassConstructTriangle{// Function to generate Special Triangle.publicstaticvoidprintTriangle(int[] A){// Base caseif(A.length <1)return;// Creating new array which contains the// Sum of consecutive elements in// the array passes as parameter.int[] temp =newint[A.length -1];for(inti =0; i < A.length -1; i++){intx = A[i] + A[i +1];temp[i] = x;}// Make a recursive call and pass// the newly created arrayprintTriangle(temp);// Print current array in the end so// that smaller arrays are printed firstSystem.out.println(Arrays.toString(A));}// Driver functionpublicstaticvoidmain(String[] args){int[] A = {1,2,3,4,5};printTriangle(A);}}Python3
# Python3 program to create Special triangle.# Function to generate Special Triangle.defprintTriangle(A):# Base caseif(len(A) <1):return# Creating new array which contains the# Sum of consecutive elements in# the array passes as parameter.temp=[0]*(len(A)-1)foriinrange(0,len(A)-1):x=A[i]+A[i+1]temp[i]=x# Make a recursive call and pass# the newly created arrayprintTriangle(temp)# Print current array in the end so# that smaller arrays are printed firstprint(A)# Driver functionA=[1,2,3,4,5]printTriangle(A)# This code is contributed by Smitha Dinesh SemwalC#
// C# program to create Special triangle.usingSystem;publicclassConstructTriangle{// Function to generate Special TrianglestaticvoidprintTriangle(int[]A,intn){// Base caseif(n < 1)return;// Creating new array which contains the// Sum of consecutive elements in// the array passes as parameter.int[]temp =newint[n - 1];for(inti = 0; i < n - 1; i++){intx = A[i] + A[i + 1];temp[i] = x;}// Make a recursive call and pass// the newly created arrayprintTriangle(temp, n - 1);// Print current array in the end so// that smaller arrays are printed firstfor(inti = 0; i < n ; i++){if(i == n - 1)Console.Write(A[i] +" ");elseConsole.Write(A[i] +", ");}Console.WriteLine();}// Driver functionpublicstaticvoidMain(){int[] A = { 1, 2, 3, 4, 5 };intn = A.Length;printTriangle(A,n);}}//This code contributed by 29AjayKumarPHP
<?php// PHP program to create// Special triangle.// Function to generate// Special TrianglefunctionprintTriangle($A,$n){// Base caseif($n< 1)return;// Creating new array which// contains the Sum of// consecutive elements in// the array passes as parameter.$temp[$n- 1] = 0;for($i= 0;$i<$n- 1;$i++){$x=$A[$i] +$A[$i+ 1];$temp[$i] =$x;}// Make a recursive call and// pass the newly created arrayprintTriangle($temp,$n- 1);// Print current array in the// end so that smaller arrays// are printed firstfor($i= 0;$i<$n;$i++){if($i==$n- 1)echo$A[$i] ," ";elseecho$A[$i] ,", ";}echo"\n";}// Driver Code$A=array( 1, 2, 3, 4, 5 );$n= sizeof($A);printTriangle($A,$n);// This code is contributed// by nitin mittal.?>Output :
[48] [20, 28] [8, 12, 16] [3, 5, 7, 9] [1, 2, 3, 4, 5]
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.


