algorithms_and_data_structures icon indicating copy to clipboard operation
algorithms_and_data_structures copied to clipboard

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

Open harshit078 opened this issue 4 years ago • 3 comments

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 avatar Jun 30 '21 13:06 harshit078

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

harshit078 avatar Jun 30 '21 13:06 harshit078

how to work this

On Wed, 30 Jun 2021 at 19:05, Harshit Singh @.***> wrote:

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

}

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/mandliya/algorithms_and_data_structures/issues/189#issuecomment-871409758, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASKBFYL4AYXDTTMA54NA7ALTVMMSBANCNFSM47SLIOGA .

socialm34 avatar Jul 05 '21 10:07 socialm34

how to work this

using VS code

harshit078 avatar Jul 06 '21 13:07 harshit078