Longest Even Odd Subarray With Threshold
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 where 0 <= l <= r < nums.length that satisfies the following conditions:
nums[l] % 2 == 0- For all indices
iin the range[l, r - 1],nums[i] % 2 != nums[i + 1] % 2 - For all indices
iin 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.
Example 1
Input
nums = [3,2,5,4], threshold = 5Output
3The subarray
[2, 5, 4] starts at l = 1, ends at r = 3, satisfies all conditions, and has the maximum possible length 3.Example 2
Input
nums = [1,2], threshold = 2Output
1The subarray
[2] starts and ends at index 1, satisfies all conditions, and has the maximum possible length 1.Constraints
- 1 <= nums.length <= 100
- 1 <= nums[i] <= 100
- 1 <= threshold <= 100