Longest Strictly Increasing Contiguous Run
Arrays & HashingAuto-graded · 18 test casesLast updated
Tests whether a candidate can scan an array in a single linear pass to find the length of the longest strictly increasing contiguous run.
Problem
You are given an integer array. Your task is to find the length of the longest contiguous segment of elements where each value is strictly greater than the one before it.
A single element always forms a valid run of length 1. If the array is empty, return 0.
The comparison is strict: two adjacent equal values break the run, just as a decrease would.
Do not confuse this with the Longest Increasing Subsequence problem. Here the elements must be adjacent in the original array — no skipping allowed.
Constraints
0 <= len(nums) <= 10^5-10^9 <= nums[i] <= 10^9- The array may be empty, contain a single element, or consist entirely of equal or decreasing values.
- Only the length of the longest run is required, not the run itself.
Examples
Example 1
Input: longest_strict_run([1, 2, 3, 2, 4, 5])
Output: 3
Example 2
Input: longest_strict_run([5, 4, 3, 2, 1])
Output: 1
Example 3
Input: longest_strict_run([1, 2, 2, 3, 4, 0, 1, 2, 3])
Output: 4
Hints
Hint 1
Try maintaining a counter for the run currently in progress. What resets it, and what extends it?
Hint 2
You only need to compare each element to the one immediately before it. One pass through the array is enough.
Hint 3
A common bug: forgetting to update your best-so-far on every step, not just when a run ends. A run that reaches the last element must still be counted.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key observation is that every strictly increasing contiguous run is fully described by where it ends. At each index i, there is exactly one longest strictly increasing run that ends at i — it either extends the run ending at i - 1 (if nums[i] > nums[i-1]) or it starts fresh as a run of length 1 (if not). A single left-to-right pass is therefore sufficient.
Algorithm
Keep two integers:
current— the length of the strictly increasing run ending at the current index.best— the largest valuecurrenthas reached so far.
At each step:
- If
nums[i] > nums[i - 1], incrementcurrent. - Otherwise, reset
currentto1. - Update
best = max(best, current)on every iteration.
Updating best inside the loop — not only when a run breaks — is critical. A run that extends all the way to the last element would otherwise go unrecorded.
Why it is correct
Invariant: after processing index i, current holds the length of the longest strictly increasing contiguous subarray ending exactly at i.
- Base case (
i = 0): the only subarray ending at index 0 is the single element itself, socurrent = 1. ✓ - Inductive step: a strictly increasing subarray of length ≥ 2 ending at
imust includenums[i - 1]as its penultimate element, sonums[i] > nums[i - 1]is required. When that holds, appendingnums[i]to the best run ending ati - 1gives the best run ending ati. When it does not hold, no multi-element increasing run can end ati, so the answer is 1. ✓
Because every contiguous subarray ends at some index, the overall answer is the maximum of current across all indices, which is exactly what best records.
Edge cases
| Input | Output | Reason |
|---|---|---|
[] | 0 | Empty array; handled by the early return. |
[7] | 1 | A single element is trivially a run of length 1. |
[3, 3, 3] | 1 | Equal neighbours are not strictly increasing; every step resets current. |
[5, 4, 3] | 1 | Strictly decreasing; no run of length 2 exists. |
[1, 2, 3, 4] | 4 | The whole array is one run. |
Complexity
- Time: O(n) — one pass, constant work per element.
- Space: O(1) — only two integer counters regardless of input size.
This is optimal: any correct solution must inspect every element, since changing a single value can change the answer.
Recovering the actual indices
If you need to return the subarray itself rather than its length, track where the current run started and record that start whenever best improves:
from typing import List, Tuple
def longest_strict_run_indices(nums: List[int]) -> Tuple[int, int]:
"""Returns the inclusive (start, end) indices of the first longest run."""
if not nums:
return (-1, -1)
best, best_start = 1, 0
current, start = 1, 0
for i in range(1, len(nums)):
if nums[i] > nums[i - 1]:
current += 1
else:
current, start = 1, i
if current > best:
best, best_start = current, start
return (best_start, best_start + best - 1)
Using strict > when updating best returns the first longest run. Switching to >= returns the last one — a tie-breaking detail worth mentioning.
Reference solution
from typing import List
def longest_strict_run(nums: List[int]) -> int:
if not nums:
return 0
best = 1
current = 1
for i in range(1, len(nums)):
if nums[i] > nums[i - 1]:
current += 1
else:
current = 1
if current > best:
best = current
return best