Files
pplus-library/pseudo/programs/19-max-subarray.pseudo
T
alexm 568474d52f
Test (in pplus-core) / in-core (push) Failing after 28m44s
Initial commit
2026-11-12 22:23:48 -05:00

17 lines
471 B
Plaintext

// Kadane: find the best, with an early return for the empty case.
// EXPECT: 6
// EXPECT: -1
FUNCTION MAX_SUBARRAY(Xs : LIST OF INTEGER) RETURNS INTEGER
REQUIRE LENGTH(Xs) >= 1
DECLARE Best, Here : INTEGER
Best <- Xs[0]
Here <- Xs[0]
FOR I <- 1 TO LENGTH(Xs)
Here <- MAX(Xs[I], Here + Xs[I])
Best <- MAX(Best, Here)
NEXT I
RETURN Best
ENDFUNCTION
OUTPUT MAX_SUBARRAY([-2, 1, -3, 4, -1, 2, 1, -5, 4])
OUTPUT MAX_SUBARRAY([-1])