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