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.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given a 0-indexed integer array nums and an integer threshold.
Find the length of the longest subarray of nums starting at index l and ending at index r (0 <= l <= r < nums.length) that satisfies the following conditions:
nums[l] % 2 == 0i in the range [l, r - 1], nums[i] % 2 != nums[i + 1] % 2i in the range [l, r], nums[i] <= thresholdReturn an integer denoting the length of the longest such subarray.
Note: A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [3,2,5,4], threshold = 5 Output: 3 Explanation: In this example, we can select the subarray that starts at l = 1 and ends at r = 3 => [2,5,4]. This subarray satisfies the conditions. Hence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length.
Example 2:
Input: nums = [1,2], threshold = 2 Output: 1 Explanation: In this example, we can select the subarray that starts at l = 1 and ends at r = 1 => [2]. It satisfies all the conditions and we can show that 1 is the maximum possible achievable length.
Example 3:
Input: nums = [2,3,4,5], threshold = 4 Output: 3 Explanation: In this example, we can select the subarray that starts at l = 0 and ends at r = 2 => [2,3,4]. It satisfies all the conditions. Hence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length.
Constraints:
1 <= nums.length <= 100 1 <= nums[i] <= 100 1 <= threshold <= 100Problem summary: You are given a 0-indexed integer array nums and an integer threshold. Find the length of the longest subarray of nums starting at index l and ending at index r (0 <= l <= r < nums.length) that satisfies the following conditions: nums[l] % 2 == 0 For all indices i in the range [l, r - 1], nums[i] % 2 != nums[i + 1] % 2 For all indices i in the range [l, r], nums[i] <= threshold Return an integer denoting the length of the longest such subarray. Note: A subarray is a contiguous non-empty sequence of elements within an array.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Sliding Window
[3,2,5,4] 5
[1,2] 2
[2,3,4,5] 4
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2760: Longest Even Odd Subarray With Threshold
class Solution {
public int longestAlternatingSubarray(int[] nums, int threshold) {
int ans = 0, n = nums.length;
for (int l = 0; l < n; ++l) {
if (nums[l] % 2 == 0 && nums[l] <= threshold) {
int r = l + 1;
while (r < n && nums[r] % 2 != nums[r - 1] % 2 && nums[r] <= threshold) {
++r;
}
ans = Math.max(ans, r - l);
}
}
return ans;
}
}
// Accepted solution for LeetCode #2760: Longest Even Odd Subarray With Threshold
func longestAlternatingSubarray(nums []int, threshold int) (ans int) {
n := len(nums)
for l := range nums {
if nums[l]%2 == 0 && nums[l] <= threshold {
r := l + 1
for r < n && nums[r]%2 != nums[r-1]%2 && nums[r] <= threshold {
r++
}
ans = max(ans, r-l)
}
}
return
}
# Accepted solution for LeetCode #2760: Longest Even Odd Subarray With Threshold
class Solution:
def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:
ans, n = 0, len(nums)
for l in range(n):
if nums[l] % 2 == 0 and nums[l] <= threshold:
r = l + 1
while r < n and nums[r] % 2 != nums[r - 1] % 2 and nums[r] <= threshold:
r += 1
ans = max(ans, r - l)
return ans
// Accepted solution for LeetCode #2760: Longest Even Odd Subarray With Threshold
// 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 #2760: Longest Even Odd Subarray With Threshold
// class Solution {
// public int longestAlternatingSubarray(int[] nums, int threshold) {
// int ans = 0, n = nums.length;
// for (int l = 0; l < n; ++l) {
// if (nums[l] % 2 == 0 && nums[l] <= threshold) {
// int r = l + 1;
// while (r < n && nums[r] % 2 != nums[r - 1] % 2 && nums[r] <= threshold) {
// ++r;
// }
// ans = Math.max(ans, r - l);
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #2760: Longest Even Odd Subarray With Threshold
function longestAlternatingSubarray(nums: number[], threshold: number): number {
const n = nums.length;
let ans = 0;
for (let l = 0; l < n; ++l) {
if (nums[l] % 2 === 0 && nums[l] <= threshold) {
let r = l + 1;
while (r < n && nums[r] % 2 !== nums[r - 1] % 2 && nums[r] <= threshold) {
++r;
}
ans = Math.max(ans, r - l);
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
For each starting index, scan the next k elements to compute the window aggregate. There are n−k+1 starting positions, each requiring O(k) work, giving O(n × k) total. No extra space since we recompute from scratch each time.
The window expands and contracts as we scan left to right. Each element enters the window at most once and leaves at most once, giving 2n total operations = O(n). Space depends on what we track inside the window (a hash map of at most k distinct elements, or O(1) for a fixed-size window).
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: Using `if` instead of `while` leaves the window invalid for multiple iterations.
Usually fails on: Over-limit windows stay invalid and produce wrong lengths/counts.
Fix: Shrink in a `while` loop until the invariant is valid again.