sumtriangle

#include<stdio.h>

 int tri(int,int,int);
 int a[100][100];
 int s1,s2,val;
 main()
 {  
	int m,n,i,j,sum,k;
	scanf("%d",&m);
	for(k=1;k<=m;k++)
	{
		scanf("%d",&n);
		for(i=0;i<n;i++)
		{ for(j=0;j<=i;j++)
		scanf("%d",&a[i][j]);
		printf("\n");
     }
	sum=tri(0,0,n);
	printf("%d",sum);
		
	}
		
	
		
		return 0;
		}
		int tri(int i,int j,int n)
		{
			if(i>=(n-1))
		return a[i][j];
		else{
			s1=a[i][j]+tri(i+1,j,n);
			s2=a[i][j]+tri(i+1,j+1,n);
		   val=(s1>=s2)?s1:s2;
		   return val;
		   	
	}
}

The following errors are present in the following code :

  1. Number of rows are less than 100. So the input array should be of size 100 * 100.

  2. remove getch().

  3. remove #include conio.h

The following changes will start giving up correct answers but as the input can be of size 100 rows and you are recursively checking all the cases possible and then finding the maximum value would give TLE. So try using a dynamic programming approach to solve the problem.

1 Like

that is also not working

//