Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hi everyone, I would like to add one question which would be a gainer to the bar of practise in array. #189

Open
harshit078 opened this issue Jun 30, 2021 · 3 comments

Comments

@harshit078
Copy link

The Question is - 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.

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)

@harshit078
Copy link
Author

#include
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);
}

@socialm34
Copy link

socialm34 commented Jul 5, 2021 via email

@harshit078
Copy link
Author

how to work this

using VS code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants