Professional Documents
Culture Documents
Codechef Beginner qn4
Codechef Beginner qn4
C Programming
Code-Chef Submission
QUESTION: 4: SUMTRIAN
BEGINNER
on each path the next number is located on the row below, more
precisely either directly below or below and one place to the right;
//ANANDHA KRISHNAN.R
#include<stdio.h>
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,i,j,y,x;
scanf("%d",&n);
int a[n][n];
for(i=0;i<n;i++)
for(j=0;j<i+1;j++)
scanf("%d",&a[i][j]);
for(i=n-2;i>=0;i--)
for(j=0;j<n-1;j++)
{
x=a[i][j]+a[i+1][j];
y=a[i][j]+a[i+1][j+1];
a[i][j]=x>y?x:y;
}
printf("%d\n",a[0][0]);
}
return 0;
}
APPROACH:
First enter the number of test cases to be executed ,then enter the
number of lines of the triangle .Then enter the values in each line of
the triangle(which will be stored as an array).Then the program will
check for the greatest sum from the second last line and last line and
saves those values the array value of second last line. Then it
compares the third last line and second last line and so on. And
finally will print the value of a[0][0] , which will have the largest sum