Problem
You are given an integer array nums (length n).
Part 1
Find the length of the longest contiguous subarray that is strictly increasing (i.e., for every adjacent pair in the subarray, a[i] < a[i+1]).
Input: nums: int[]
Output: maxLen: int
Example:
-
nums = [1, 2, 2, 3, 4]
→ longest strictly increasing contiguous subarray is
[2,3,4]
, so
maxLen = 3
.
Follow-up
Now suppose you may modify (replace) the value of at most one element within a chosen contiguous subarray (replace it with any integer value you want) in order to make that subarray strictly increasing.
Return the maximum possible length of a contiguous subarray that can be made strictly increasing with ≤ 1 replacement.
Clarification/assumption (make explicit in interview): The replacement value is unconstrained (can be any integer), and you are asked only for the maximum achievable length.
Example idea:
-
nums = [1, 5, 3, 4]
→ by replacing
5
with
2
, the whole array can become
[1,2,3,4]
, so answer is
4
.
Constraints (reasonable interview defaults)
-
1 <= n <= 2e5
-
-1e9 <= nums[i] <= 1e9
-
Aim for better than
O(n^2)
in the follow-up.