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 <= 100When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a long row of numbers and you need to find the longest group sitting next to each other where every number is below a certain limit, and the types of numbers alternate between one kind and another. The brute force approach simply tests every single possible group of numbers in the entire row to see if it follows all the rules.
Here's how the algorithm would work step-by-step:
def longest_even_odd_subarray(nums, threshold):
max_length = 0
# We test every possible starting position to ensure we do not miss any valid subarray
for start_index in range(len(nums)):
if nums[start_index] <= threshold and nums[start_index] % 2 == 0:
current_length = 1
max_length = max(max_length, current_length)
for current_index in range(start_index + 1, len(nums)):
# Check bounds and limit rule before verifying the alternating parity requirement
if nums[current_index] <= threshold and (nums[current_index] % 2 != nums[current_index - 1] % 2):
current_length += 1
max_length = max(max_length, current_length)
else:
break
# Return the largest valid group length discovered across all checked starting points
return max_length
Instead of checking every single possible grouping of numbers, the optimal strategy walks through the sequence and builds valid alternating patterns on the fly. Whenever a rule is broken, it instantly abandons the current attempt, remembers the longest successful stretch seen so far, and starts looking for a new pattern.
Here's how the algorithm would work step-by-step:
class Solution:
def longest_even_odd_subarray(self, nums: list[int], threshold: int) -> int:
max_length_record = 0
array_index = 0
while array_index < len(nums):
# A valid start requires an even number that does not exceed the threshold limit.
if nums[array_index] % 2 == 0 and nums[array_index] <= threshold:
current_chain_length = 1
next_index = array_index + 1
while next_index < len(nums):
# Elements must alternate parity and stay within threshold to grow the chain.
if nums[next_index] <= threshold and nums[next_index] % 2 != nums[next_index - 1] % 2:
current_chain_length += 1
next_index += 1
else:
break
# Keep track of the longest valid alternating sequence found across attempts.
if current_chain_length > max_length_record:
max_length_record = current_chain_length
array_index = next_index
else:
array_index += 1
return max_length_record
| Case | How to Handle |
|---|---|
| Empty or null input array | Return zero immediately since no valid subarray can exist. |
| Single element array | Check if the single element is even and less than or equal to the threshold, returning 1 if true and 0 if false. |
| Starting element is odd | The algorithm must ignore odd starting elements because valid subarrays are explicitly required to start with an even number. |
| All elements exceed the threshold | The validation check will fail for every element, resulting in a returned length of zero. |
| No alternating parity exists in the array | The tracking loop resets whenever parity fails, ultimately returning a maximum length of zero if no valid subarray is found. |
| Negative numbers and zero included in the array | Since zero is considered even, the algorithm correctly evaluates its parity and threshold condition just like positive numbers. |
| Maximum possible constraint limits with large inputs | A single-pass linear scan approach ensures optimal efficiency and avoids performance bottlenecks. |
| Elements equal to the threshold boundary | The less-than-or-equal-to condition correctly includes values that precisely match the threshold limit. |