Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2017/imperative-programming-resit/3/c/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2017 Imperative programming (resit) 3 c Solution by
Codex 0 2026-10-31
Increasing here means nondecreasing, so equal neighbors remain in the same run. Change only the suffix-reset test:
best = 0; run = 0; bestStart = 0
for i = 0,...,n-1:
if i == 0 or a[i] < a[i-1]: run = 1
else: run = run+1
if run > best:
best = run; bestStart = i-run+1
return (bestStart,best)After index ,
run is the longest nondecreasing suffix of the prefix and the recorded interval is a longest nondecreasing segment seen so far. The adjacent-comparison condition implies the all-pairs condition by transitivity. The scan uses time and auxiliary space. Empty input yields length zero because no nonempty increasing segment exists. New to topics? Read the docs here!