← Back to problems
UNSW COMP2521 · easy

Arrays: Longest Strictly-Increasing Run

Arrays: length of the longest strictly-increasing contiguous run

Read a list of integers and find the length of the longest contiguous run that is strictly increasing.

  • Contiguous: it must be an adjacent stretch (indices i, i+1, i+2 ...), not cherry-picked.
  • Strictly increasing: each element must be greater than the previous one (a[i] > a[i-1]); equal values do not count.

For example 1 3 2 4 5 6 1: the run 2 4 5 6 has length 4 and is the longest.

Complete longestRun: keep cur for how long the current run has been increasing and best for the longest seen. Scan from i = 1 — if a[i] > a[i-1] do cur++, otherwise the run broke so reset cur to 1; refresh best with cur every step.

Pitfalls (straight from the bug lab): (1) writing the loop as i <= n reads the out-of-bounds garbage at a[n]; valid indices are 0 ~ n-1. (2) Writing >= instead of the strict > counts equal values as increasing and overshoots. (3) When n == 0 the answer is 0 — do not return a best that started at 1.

Input

  • Line 1: integer n (count, may be 0).
  • Line 2: n space-separated integers.

Output

  • One line: the length of the longest strictly-increasing contiguous run.

Example: input 7 / 1 3 2 4 5 6 1 -> 4.