Longest Increasing Subarray with One Modification

Quick Overview

Find the length of the longest strictly increasing contiguous subarray of an integer array after changing exactly one element to any integer you choose. It tests linear-time array scanning, careful case analysis around the one changed element, and edge cases at the array boundaries.

Longest Increasing Subarray with One Modification

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

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 ```python 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 ```text 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. ```text 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. ```text Input: nums = [1, 2, 9, 4, 5] Output: 5 ``` Changing 9 to 3 gives [1, 2, 3, 4, 5], which uses the whole array.

Overview: Find the length of the longest strictly increasing contiguous subarray of an integer array after changing exactly one element to any integer you choose. It tests linear-time array scanning, careful case analysis around the one changed element, and edge cases at the array boundaries.

Community answers

Answer by fishtank299

int longest_substring(vector& v){ if(v.empty())return 0; if(v.size()==1)return 1; int start = 0; int end = 1; vector> tmp; while(endv[end-1]){ end++; }else{ tmp.push_back({start, end-1}); start = end; end = end+1; } } if(start(tmp[i-1]); int prev_end = get<1>(tmp[i-1]); int next_start = get<0>(tmp[i]); int next_end = get<1>(tmp[i]); // change next_start if(next_start==next_end || v[prev_end]+1v[prev_end-1]+1){ // change prev_end g_len = max(g_len, next_end-prev_start+1); }else{ g_len = max(g_len, max(next_end-next_start+2, prev_end-prev_start+2)); } i++; } return g_len; }
Google logo
Google
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
3
0

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.