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.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You are given an integer array nums.
A subarray nums[l..r] is alternating if one of the following holds:
nums[l] < nums[l + 1] > nums[l + 2] < nums[l + 3] > ...nums[l] > nums[l + 1] < nums[l + 2] > nums[l + 3] < ...In other words, if we compare adjacent elements in the subarray, then the comparisons alternate between strictly greater and strictly smaller.
You can remove at most one element from nums. Then, you select an alternating subarray from nums.
Return an integer denoting the maximum length of the alternating subarray you can select.
A subarray of length 1 is considered alternating.
Example 1:
Input: nums = [2,1,3,2]
Output: 4
Explanation:
[2, 1, 3, 2], which is alternating because 2 > 1 < 3 > 2.Example 2:
Input: nums = [3,2,1,2,3,2,1]
Output: 4
Explanation:
nums[3] i.e., [3, 2, 1, 2, 3, 2, 1]. The array becomes [3, 2, 1, 3, 2, 1].[3, 2, 1, 3, 2, 1].Example 3:
Input: nums = [100000,100000]
Output: 1
Explanation:
[100000, 100000].Constraints:
2 <= nums.length <= 1051 <= nums[i] <= 105Problem summary: You are given an integer array nums. A subarray nums[l..r] is alternating if one of the following holds: nums[l] < nums[l + 1] > nums[l + 2] < nums[l + 3] > ... nums[l] > nums[l + 1] < nums[l + 2] > nums[l + 3] < ... In other words, if we compare adjacent elements in the subarray, then the comparisons alternate between strictly greater and strictly smaller. You can remove at most one element from nums. Then, you select an alternating subarray from nums. Return an integer denoting the maximum length of the alternating subarray you can select. A subarray of length 1 is considered alternating.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[2,1,3,2]
[3,2,1,2,3,2,1]
[100000,100000]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3830: Longest Alternating Subarray After Removing At Most One Element
class Solution {
public int longestAlternating(int[] nums) {
int n = nums.length;
int[] l1 = new int[n];
int[] l2 = new int[n];
int[] r1 = new int[n];
int[] r2 = new int[n];
for (int i = 0; i < n; i++) {
l1[i] = 1;
l2[i] = 1;
r1[i] = 1;
r2[i] = 1;
}
int ans = 0;
for (int i = 1; i < n; i++) {
if (nums[i - 1] < nums[i]) {
l1[i] = l2[i - 1] + 1;
} else if (nums[i - 1] > nums[i]) {
l2[i] = l1[i - 1] + 1;
}
ans = Math.max(ans, l1[i]);
ans = Math.max(ans, l2[i]);
}
for (int i = n - 2; i >= 0; i--) {
if (nums[i + 1] > nums[i]) {
r1[i] = r2[i + 1] + 1;
} else if (nums[i + 1] < nums[i]) {
r2[i] = r1[i + 1] + 1;
}
}
for (int i = 1; i < n - 1; i++) {
if (nums[i - 1] < nums[i + 1]) {
ans = Math.max(ans, l2[i - 1] + r2[i + 1]);
} else if (nums[i - 1] > nums[i + 1]) {
ans = Math.max(ans, l1[i - 1] + r1[i + 1]);
}
}
return ans;
}
}
// Accepted solution for LeetCode #3830: Longest Alternating Subarray After Removing At Most One Element
func longestAlternating(nums []int) int {
n := len(nums)
l1 := make([]int, n)
l2 := make([]int, n)
r1 := make([]int, n)
r2 := make([]int, n)
for i := 0; i < n; i++ {
l1[i] = 1
l2[i] = 1
r1[i] = 1
r2[i] = 1
}
ans := 0
for i := 1; i < n; i++ {
if nums[i-1] < nums[i] {
l1[i] = l2[i-1] + 1
} else if nums[i-1] > nums[i] {
l2[i] = l1[i-1] + 1
}
ans = max(ans, l1[i], l2[i])
}
for i := n - 2; i >= 0; i-- {
if nums[i+1] > nums[i] {
r1[i] = r2[i+1] + 1
} else if nums[i+1] < nums[i] {
r2[i] = r1[i+1] + 1
}
}
for i := 1; i < n-1; i++ {
if nums[i-1] < nums[i+1] {
if l2[i-1]+r2[i+1] > ans {
ans = l2[i-1] + r2[i+1]
}
} else if nums[i-1] > nums[i+1] {
if l1[i-1]+r1[i+1] > ans {
ans = l1[i-1] + r1[i+1]
}
}
}
return ans
}
# Accepted solution for LeetCode #3830: Longest Alternating Subarray After Removing At Most One Element
class Solution:
def longestAlternating(self, nums: List[int]) -> int:
n = len(nums)
l1 = [1] * n
l2 = [1] * n
r1 = [1] * n
r2 = [1] * n
ans = 0
for i in range(1, n):
if nums[i - 1] < nums[i]:
l1[i] = l2[i - 1] + 1
elif nums[i - 1] > nums[i]:
l2[i] = l1[i - 1] + 1
ans = max(ans, l1[i], l2[i])
for i in range(n - 2, -1, -1):
if nums[i + 1] > nums[i]:
r1[i] = r2[i + 1] + 1
elif nums[i + 1] < nums[i]:
r2[i] = r1[i + 1] + 1
for i in range(1, n - 1):
if nums[i - 1] < nums[i + 1]:
ans = max(ans, l2[i - 1] + r2[i + 1])
elif nums[i - 1] > nums[i + 1]:
ans = max(ans, l1[i - 1] + r1[i + 1])
return ans
// Accepted solution for LeetCode #3830: Longest Alternating Subarray After Removing At Most One Element
impl Solution {
pub fn longest_alternating(nums: Vec<i32>) -> i32 {
let n = nums.len();
let mut l1 = vec![1; n];
let mut l2 = vec![1; n];
let mut r1 = vec![1; n];
let mut r2 = vec![1; n];
let mut ans = 0;
for i in 1..n {
if nums[i - 1] < nums[i] {
l1[i] = l2[i - 1] + 1;
} else if nums[i - 1] > nums[i] {
l2[i] = l1[i - 1] + 1;
}
ans = ans.max(l1[i]);
ans = ans.max(l2[i]);
}
for i in (0..n - 1).rev() {
if nums[i + 1] > nums[i] {
r1[i] = r2[i + 1] + 1;
} else if nums[i + 1] < nums[i] {
r2[i] = r1[i + 1] + 1;
}
}
for i in 1..n - 1 {
if nums[i - 1] < nums[i + 1] {
ans = ans.max(l2[i - 1] + r2[i + 1]);
} else if nums[i - 1] > nums[i + 1] {
ans = ans.max(l1[i - 1] + r1[i + 1]);
}
}
ans
}
}
// Accepted solution for LeetCode #3830: Longest Alternating Subarray After Removing At Most One Element
function longestAlternating(nums: number[]): number {
const n = nums.length;
const l1 = new Array<number>(n).fill(1);
const l2 = new Array<number>(n).fill(1);
const r1 = new Array<number>(n).fill(1);
const r2 = new Array<number>(n).fill(1);
let ans = 0;
for (let i = 1; i < n; i++) {
if (nums[i - 1] < nums[i]) {
l1[i] = l2[i - 1] + 1;
} else if (nums[i - 1] > nums[i]) {
l2[i] = l1[i - 1] + 1;
}
ans = Math.max(ans, l1[i]);
ans = Math.max(ans, l2[i]);
}
for (let i = n - 2; i >= 0; i--) {
if (nums[i + 1] > nums[i]) {
r1[i] = r2[i + 1] + 1;
} else if (nums[i + 1] < nums[i]) {
r2[i] = r1[i + 1] + 1;
}
}
for (let i = 1; i < n - 1; i++) {
if (nums[i - 1] < nums[i + 1]) {
ans = Math.max(ans, l2[i - 1] + r2[i + 1]);
} else if (nums[i - 1] > nums[i + 1]) {
ans = Math.max(ans, l1[i - 1] + r1[i + 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.