Solution

ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2017/imperative-programming-resit/3/b/solution

Add bestStart = 0 initially and, whenever a strictly longer run is found, record
best = run
bestStart = i-run+1
Then 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.

New to topics? Read the docs here!