Given an integer array nums, find the length of the longest strictly increasing subarray (contiguous elements) where you are allowed to change exactly one element to any integer you choose.
You must use exactly one modification. The subarray must be contiguous, and after the modification, every consecutive pair in the subarray must be strictly increasing.
Function Signature
def longest_increasing_subarray(nums: list[int]) -> int:
Rules
-
The new value must be an integer. It may be any integer, including one outside the range of the original values.
-
Strictly increasing means each element is greater than the one before it. Equal neighbors break a run.
-
Return only the length. A single element always qualifies, so the answer is at least 1.
Constraints
-
1 <= nums.length <= 10^5
-
-10^9 <= nums[i] <= 10^9
Examples
Input: nums = [1, 2, 10, 3, 4, 5]
Output: 4
No integer lies strictly between 2 and 3, so changing 10 cannot join [1, 2] to [3, 4, 5]. Changing 10 to 2 gives the run [2, 3, 4, 5], and no window of length 5 can be repaired with a single change.
Input: nums = [1, 2, 3, 5, 7, 8]
Output: 6
The array is already strictly increasing. The required change can keep it that way (for example, 5 becomes 4), so the whole array counts.
Input: nums = [1, 2, 9, 4, 5]
Output: 5
Changing 9 to 3 gives [1, 2, 3, 4, 5], which uses the whole array.