Solution (source code)

= Solution

Add `bestStart = 0` initially and, whenever a strictly longer run is found, record

``
best = run
bestStart = i-run+1
``

Then $a[\mathrm{bestStart}:\mathrm{bestStart}+\mathrm{best})$ is a plateau of the returned maximum length. Keeping the old answer on equal lengths selects the earliest maximum. The <loop invariant> from part (a) gains the assertion that the recorded interval realizes `best`; time and auxiliary-space bounds are unchanged. For empty input use the empty interval at zero.