sum in a triangle. the code chef is giving wrong answer for my code but it is matching with the sample output given in the question...please tell whats wrong with my code...:(
Explain the logic ?
I have taken a two-dimensional array to make the triangle.First, I have taken n number of test cases then in each test case t number of lines are taken i.e the number of rows in each array would be t.Then I have taken input in such a way that the values get entered in a triangle.
NOW, for output I have taken a variable sum which is a array,and I made the values of all its elements zero.Then I started finding the sum form the last element of the last row in each triangle to the first element of the first row then from second last element to the first element and so on till the last element of first row to the first element of first row.
Consider the value of t=3
then for k=0,the value of l starts form 2 to 0,
for j=2 and l=2
sum= 0 + a
then for j-- and l–,j=1,l=1
Then similarly for k=1
when we have j=0 and we have to do j–,the value of j will become -1,so I have considered that if j<0,j=j+1,so that the value of j will be zero and I will get the desired value.
i.e for k=1, I would have
then for k=2,j=0
now we have a array of sum of all the paths in the triangle.
Then I found the largest of them.
My code is running very well on my pc and is giving the right answer,but i don’t know why code chef is giving me wrong answer…please check whether my code is correct or not.
I have explained it below in the answer.
Did u consider this case? 00->11->21
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;
According to your logic the test cases give correct answer but consider – if 3rd row 2nd element was 5.then path would be 1->1->5=7
Now I have modified my code but its still not working…
http://www.codechef.com/viewsolution/4202484 my code is here on this link. Please check whats wrong in it. The logic is same as that of earlier but the change is that the paths which I was not taking earlier, I have taken another loop for them. Please check my code once…because I am not getting whats wrong now.