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 :

  1. Recursion is the key. At each iteration create a new array which contains the Sum of consecutive elements in the array passes as parameter.
  2. Make a recursive call and pass the newly created array in the previous step.
  3. While back tracking print the array (for printing in reverse order).
  4.  
    Below is implementation of the above approach :

    C++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to create Special triangle.
    #include<bits/stdc++.h>
    using namespace std;
      
    // Function to generate Special Triangle
    void printTriangle(int A[] , int n)
        {
            // Base case
            if (n < 1)
                return;
      
            // Creating new array which contains the
            // Sum of consecutive elements in
            // the array passes as parameter.
            int temp[n - 1];
            for (int i = 0; i < n - 1; i++)
            {
                int x = A[i] + A[i + 1];
                temp[i] = x;
            }
      
            // Make a recursive call and pass
            // the newly created array
            printTriangle(temp, n - 1);
      
            // Print current array in the end so
            // that smaller arrays are printed first
            for (int i = 0; i < n ; i++)
            {
                if(i == n - 1)
                    cout << A[i] << " ";
                else
                cout << A[i] << ", ";
            }
                      
            cout << endl;
        }
      
        // Driver function
        int main()
        {
            int A[] = { 1, 2, 3, 4, 5 };
            int n = sizeof(A) / sizeof(A[0]);
              
            printTriangle(A, n);
        }
          
    // This code is contributed by Smitha Dinesh Semwal

    chevron_right

    
    

    Java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to create Special triangle.
    import java.util.*;
    import java.lang.*;
      
    public class ConstructTriangle
    {
        // Function to generate Special Triangle.
        public static void printTriangle(int[] A)
        {
            // Base case
            if (A.length < 1)
                return;
      
            // Creating new array which contains the
            // Sum of consecutive elements in
            // the array passes as parameter.
            int[] temp = new int[A.length - 1];
            for (int i = 0; i < A.length - 1; i++)
            {
                int x = A[i] + A[i + 1];
                temp[i] = x;
            }
      
            // Make a recursive call and pass
            // the newly created array
            printTriangle(temp);
      
            // Print current array in the end so
            // that smaller arrays are printed first
            System.out.println(Arrays.toString(A));
        }
      
        // Driver function
        public static void main(String[] args)
        {
            int[] A = { 1, 2, 3, 4, 5 };
            printTriangle(A);
        }
    }

    chevron_right

    
    

    Python3

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Python3 program to create Special triangle.
    # Function to generate Special Triangle.
    def printTriangle(A):
              
            # Base case
            if (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)
            for i in range( 0, len(A) - 1):
              
                x = A[i] + A[i + 1]
                temp[i] = x
              
      
            # Make a recursive call and pass
            # the newly created array
            printTriangle(temp)
              
            # Print current array in the end so
            # that smaller arrays are printed first
            print(A)
          
      
    # Driver function
    A = [ 1, 2, 3, 4, 5 ]
    printTriangle(A)
      
    # This code is contributed by Smitha Dinesh Semwal

    chevron_right

    
    

    C#

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# program to create Special triangle.
       
    using System;
                          
    public class ConstructTriangle
    {
    // Function to generate Special Triangle
    static void printTriangle(int []A, int n)
        {
            // Base case
            if (n < 1)
                return;
       
            // Creating new array which contains the
            // Sum of consecutive elements in
            // the array passes as parameter.
            int []temp = new int[n - 1];
            for (int i = 0; i < n - 1; i++)
            {
                int x = A[i] + A[i + 1];
                temp[i] = x;
            }
       
            // Make a recursive call and pass
            // the newly created array
            printTriangle(temp, n - 1);
       
            // Print current array in the end so
            // that smaller arrays are printed first
            for (int i = 0; i < n ; i++)
            {
                if(i == n - 1)
                    Console.Write(A[i] + " ");
                else
                Console.Write(A[i] + ", ");
            }
                       
            Console.WriteLine();
        }
       
        // Driver function
        public static void Main()
        {
            int[] A = { 1, 2, 3, 4, 5 };
            int n = A.Length;
            printTriangle(A,n);
        }
    }
      
    //This code contributed by 29AjayKumar

    chevron_right

    
    

    PHP

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
    // PHP program to create
    // Special triangle.
      
    // Function to generate
    // Special Triangle
    function printTriangle($A , $n)
        {
          
            // Base case
            if ($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 array
            printTriangle($temp, $n - 1);
      
            // Print current array in the
            // end so that smaller arrays 
            // are printed first
            for ($i = 0; $i < $n ; $i++)
            {
                if($i == $n - 1)
                    echo $A[$i] , " ";
                else
                echo $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. 
    ?>

    chevron_right

    
    

    Output :

    [48]
    [20, 28]
    [8, 12, 16]
    [3, 5, 7, 9]
    [1, 2, 3, 4, 5]
    


    My Personal Notes arrow_drop_up

    Intern at GeeksforGeeks

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



    Improved By : nitin mittal, 29AjayKumar



    Article Tags :
    Practice Tags :


    1


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.