Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an integer array nums.
You are allowed to replace at most one element in the array with any other integer value of your choice.
Return the length of the longest non-decreasing subarray that can be obtained after performing at most one replacement.
An array is said to be non-decreasing if each element is greater than or equal to its previous one (if it exists).
Example 1:
Input: nums = [1,2,3,1,2]
Output: 4
Explanation:
Replacing nums[3] = 1 with 3 gives the array [1, 2, 3, 3, 2].
The longest non-decreasing subarray is [1, 2, 3, 3], which has a length of 4.
Example 2:
Input: nums = [2,2,2,2,2]
Output: 5
Explanation:
All elements in nums are equal, so it is already non-decreasing and the entire nums forms a subarray of length 5.
Constraints:
1 <= nums.length <= 105-109 <= nums[i] <= 109Problem summary: You are given an integer array nums. You are allowed to replace at most one element in the array with any other integer value of your choice. Return the length of the longest non-decreasing subarray that can be obtained after performing at most one replacement. An array is said to be non-decreasing if each element is greater than or equal to its previous one (if it exists).
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[1,2,3,1,2]
[2,2,2,2,2]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3738: Longest Non-Decreasing Subarray After Replacing at Most One Element
class Solution {
public int longestSubarray(int[] nums) {
int n = nums.length;
int[] left = new int[n];
int[] right = new int[n];
Arrays.fill(left, 1);
Arrays.fill(right, 1);
int ans = 1;
for (int i = 1; i < n; i++) {
if (nums[i] >= nums[i - 1]) {
left[i] = left[i - 1] + 1;
ans = Math.max(ans, left[i]);
}
}
for (int i = n - 2; i >= 0; i--) {
if (nums[i] <= nums[i + 1]) {
right[i] = right[i + 1] + 1;
}
}
for (int i = 0; i < n; i++) {
int a = (i - 1 < 0) ? 0 : left[i - 1];
int b = (i + 1 >= n) ? 0 : right[i + 1];
if (i - 1 >= 0 && i + 1 < n && nums[i - 1] > nums[i + 1]) {
ans = Math.max(ans, Math.max(a + 1, b + 1));
} else {
ans = Math.max(ans, a + b + 1);
}
}
return ans;
}
}
// Accepted solution for LeetCode #3738: Longest Non-Decreasing Subarray After Replacing at Most One Element
func longestSubarray(nums []int) int {
n := len(nums)
left := make([]int, n)
right := make([]int, n)
for i := range left {
left[i], right[i] = 1, 1
}
for i := 1; i < n; i++ {
if nums[i] >= nums[i-1] {
left[i] = left[i-1] + 1
}
}
for i := n - 2; i >= 0; i-- {
if nums[i] <= nums[i+1] {
right[i] = right[i+1] + 1
}
}
ans := slices.Max(left)
for i := 0; i < n; i++ {
a := 0
if i > 0 {
a = left[i-1]
}
b := 0
if i+1 < n {
b = right[i+1]
}
if i > 0 && i+1 < n && nums[i-1] > nums[i+1] {
ans = max(ans, max(a+1, b+1))
} else {
ans = max(ans, a+b+1)
}
}
return ans
}
# Accepted solution for LeetCode #3738: Longest Non-Decreasing Subarray After Replacing at Most One Element
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
n = len(nums)
left = [1] * n
right = [1] * n
for i in range(1, n):
if nums[i] >= nums[i - 1]:
left[i] = left[i - 1] + 1
for i in range(n - 2, -1, -1):
if nums[i] <= nums[i + 1]:
right[i] = right[i + 1] + 1
ans = max(left)
for i in range(n):
a = 0 if i - 1 < 0 else left[i - 1]
b = 0 if i + 1 >= n else right[i + 1]
if i - 1 >= 0 and i + 1 < n and nums[i - 1] > nums[i + 1]:
ans = max(ans, a + 1, b + 1)
else:
ans = max(ans, a + b + 1)
return ans
// Accepted solution for LeetCode #3738: Longest Non-Decreasing Subarray After Replacing at Most One Element
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3738: Longest Non-Decreasing Subarray After Replacing at Most One Element
// class Solution {
// public int longestSubarray(int[] nums) {
// int n = nums.length;
// int[] left = new int[n];
// int[] right = new int[n];
// Arrays.fill(left, 1);
// Arrays.fill(right, 1);
// int ans = 1;
//
// for (int i = 1; i < n; i++) {
// if (nums[i] >= nums[i - 1]) {
// left[i] = left[i - 1] + 1;
// ans = Math.max(ans, left[i]);
// }
// }
//
// for (int i = n - 2; i >= 0; i--) {
// if (nums[i] <= nums[i + 1]) {
// right[i] = right[i + 1] + 1;
// }
// }
//
// for (int i = 0; i < n; i++) {
// int a = (i - 1 < 0) ? 0 : left[i - 1];
// int b = (i + 1 >= n) ? 0 : right[i + 1];
// if (i - 1 >= 0 && i + 1 < n && nums[i - 1] > nums[i + 1]) {
// ans = Math.max(ans, Math.max(a + 1, b + 1));
// } else {
// ans = Math.max(ans, a + b + 1);
// }
// }
//
// return ans;
// }
// }
// Accepted solution for LeetCode #3738: Longest Non-Decreasing Subarray After Replacing at Most One Element
function longestSubarray(nums: number[]): number {
const n = nums.length;
const left: number[] = Array(n).fill(1);
const right: number[] = Array(n).fill(1);
for (let i = 1; i < n; i++) {
if (nums[i] >= nums[i - 1]) {
left[i] = left[i - 1] + 1;
}
}
for (let i = n - 2; i >= 0; i--) {
if (nums[i] <= nums[i + 1]) {
right[i] = right[i + 1] + 1;
}
}
let ans = Math.max(...left);
for (let i = 0; i < n; i++) {
const a = i - 1 < 0 ? 0 : left[i - 1];
const b = i + 1 >= n ? 0 : right[i + 1];
if (i - 1 >= 0 && i + 1 < n && nums[i - 1] > nums[i + 1]) {
ans = Math.max(ans, Math.max(a + 1, b + 1));
} else {
ans = Math.max(ans, a + b + 1);
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.