SUMTRIAN - Getting WA

I submitted this answer for the question SUMTRIAN. But I am getting WA. The test cases provided are working. I also tried some cases on my own. All seem to be giving the right answer. Can someone please point out the error?
Thanks!

When I add first test case at the end it returns 5, 9, 8 instead of expected 5, 9, 5…

Your problem is on first line of function maxFromTop(). Because you have condition:

if(i>n)
    return 0;

If I understand your code properly, variable i indicates rows. But rows are numbered from 0 to n-1 inclusive, so they don’t obtain row number n. So you should end, when i>=n.

if(i>=n)
    return 0;

Now the code should be correct.

1 Like

Thanks a lot for sparing your time to find this silly error!!

you’re welcome :slight_smile: