How to Solved this Puzzle on PHP

[font=Arial, Helvetica, sans-serif][size=4]Imagine you have a row of numbers like below(a triangle). By starting at the top of the triangle, find the maximum number in each line and sum them up. Example below.[/size][/font][font=Arial, Helvetica, sans-serif][size=4]

[/size][/font][font=Arial, Helvetica, sans-serif][size=4][color=#222222][font=Calibri, sans-serif][size=2][b][size=3]5

9[/size][/b][size=3] 6

4 6 8

0 7 1 5[/size][/size][/font][/color]

[color=#222222][font=Calibri, sans-serif][size=2][size=3] [/size][/size][/font][/color]

[color=#222222][font=Calibri, sans-serif][size=2][size=3]Answer I.e. 5 + 9 +8 + 7 = 29.[/size][/size][/font][/color]

[color=#222222][font=Calibri, sans-serif][size=2]


[/size][/font][font=Arial, Helvetica, sans-serif][size=4]Write a code to find the maximum total from top to bottom. Assume triangle can have at most 100000 rows. [/size][/font]

[font="Arial, Helvetica, sans-serif"][size=4]


[font="Arial, Helvetica, sans-serif"][size=4]Please Help…![/size][/font]

[font="Arial, Helvetica, sans-serif"][size=4]


[font="Arial, Helvetica, sans-serif"][size=4]


[font=Arial, Helvetica, sans-serif][size=4]


@Ankit Modi

this question does not belong in this forum. Can you explain me how is this question connected with Yii framework?

Hi Duri

I know about that but I find the solutions that’s why I post here.

[color="#006400"]/* NOTE: moved to proper section (General PHP Topics instead of General Discussion for Yii) */[/color]

That’s very easy:


$sourceData = [


    [9, 6],

    [3, 6, 8],

    [0, 7, 1, 5],


$sum = 0;

foreach ($sourceData as $row) {

    $sum += max($row);


echo $sum;