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 <= nreads the out-of-bounds garbage ata[n]; valid indices are0 ~ n-1. (2) Writing>=instead of the strict>counts equal values as increasing and overshoots. (3) Whenn == 0the answer is0— do not return abestthat started at 1.
Input
- Line 1: integer
n(count, may be0). - Line 2:
nspace-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.