Lazily Calculating value at index i O ( n )
for ( $ i = 0 ; $ i <= $ maxc ; $ i ++ ) { $ cur += $ x [ $ i ] ; if ( $ maxy < $ cur ) { $ maxy = $ cur ; $ idx = $ i ; } } echo " Maximum ▁ value ▁ is ▁ " . $ maxy . " ▁ at ▁ position ▁ " . $ idx . " STRNEWLINE " ; }