Watch 7 video solutions for Longest Alternating Subarray After Removing At Most One Element, a hard level problem involving Array, Dynamic Programming, Enumeration. This walkthrough by Rajan Keshari ( CSE - IIT Dhanbad ) has 1,291 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 Overview: Given an integer array, you want the longest contiguous subarray where adjacent elements alternate in parity (even, odd, even, odd or vice versa). You may remove at most one element anywhere in the array. The task is to compute the maximum possible length of such an alternating subarray after the optional removal.
Approach 1: Brute Force Enumeration (O(n^2) time, O(1) space)
Try removing every index i (including the option of removing nothing). For each case, scan the resulting array and compute the longest alternating subarray by checking nums[j] % 2 != nums[j+1] % 2. This directly simulates the problem but requires rebuilding or skipping one element during every scan. The algorithm performs up to n full passes over the array, giving O(n^2) time. Useful only for validating correctness or reasoning about the pattern.
Approach 2: Dynamic Programming Without Removal (O(n) time, O(n) space)
First compute the longest alternating segment ending at each index using a DP array left[i]. If nums[i] has different parity from nums[i-1], extend the previous segment; otherwise restart from length 1. This gives the best alternating subarray when no deletion happens. While it does not yet handle the removal, it builds the structural information needed to connect segments later. This pattern commonly appears in dynamic programming problems involving local transitions.
Approach 3: Prefix-Suffix Decomposition + Enumeration (O(n) time, O(n) space)
Precompute two arrays: left[i] = length of the alternating subarray ending at i, and right[i] = length of the alternating subarray starting at i. Then enumerate each index i as the element to remove. After removal, the two neighbors i-1 and i+1 become adjacent. If their parity differs, you can merge the prefix left[i-1] and suffix right[i+1]. Otherwise the merge is invalid and you take the best side individually. Track the maximum across all removal positions and also the no-removal case. This approach scans the array a constant number of times and uses simple parity checks, giving optimal O(n) time. The technique combines ideas from array preprocessing and enumeration.
Recommended for interviews: Prefix–suffix decomposition with enumeration. Interviewers expect an O(n) solution because the brute force clearly wastes repeated scans. Showing the brute force first demonstrates understanding of the constraint, while deriving prefix and suffix alternating lengths shows strong problem‑solving and DP intuition.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Removal + Scan | O(n^2) | O(1) | Small arrays or when validating logic during early problem exploration |
| DP for Alternating Lengths (No Removal) | O(n) | O(n) | Computing baseline alternating segments before considering deletion |
| Prefix-Suffix Decomposition + Enumeration | O(n) | O(n) | General optimal solution; handles one removal efficiently |