This question evaluates proficiency in array processing and algorithmic optimization, focusing on recognition of contiguous subarray properties and handling a single-value modification to achieve strict monotonicity.
You are given an integer array nums (length n).
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
.
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
.
1 <= n <= 2e5
-1e9 <= nums[i] <= 1e9
O(n^2)
in the follow-up.