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 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.

Example 1
Inputnums = [3,2,5,4], threshold = 5
Output3
The subarray [2, 5, 4] starts at l = 1, ends at r = 3, satisfies all conditions, and has the maximum possible length 3.
Example 2
Inputnums = [1,2], threshold = 2
Output1
The 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

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate