Algorithms
Easy
2760
2866
Longest Even Odd Subarray With Threshold
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 (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:

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 <= 100
"""
class Solution:
    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:
        
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.

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 <= 100


Please complete the code below to solve above prblem:
```python
class Solution:
    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:
        
```

my_solution = Solution()

test_input = { "nums": [3,2,5,4], "threshold": 5 }
assert my_solution.longestAlternatingSubarray(**test_input) == 3

test_input = { "nums": [1,2], "threshold": 2 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [2,3,4,5], "threshold": 4 }
assert my_solution.longestAlternatingSubarray(**test_input) == 3

test_input = { "nums": [1], "threshold": 1 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [2], "threshold": 2 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [3], "threshold": 3 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [4], "threshold": 1 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [5], "threshold": 3 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [6], "threshold": 5 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [7], "threshold": 2 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [8], "threshold": 1 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [9], "threshold": 7 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [10], "threshold": 7 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [1,3], "threshold": 16 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [1,6], "threshold": 2 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [1,10], "threshold": 6 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [1,10], "threshold": 7 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [2,2], "threshold": 18 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [2,4], "threshold": 7 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [2,5], "threshold": 2 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [2,6], "threshold": 15 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [2,7], "threshold": 9 }
assert my_solution.longestAlternatingSubarray(**test_input) == 2

test_input = { "nums": [2,8], "threshold": 4 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [2,8], "threshold": 8 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [2,8], "threshold": 16 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [2,9], "threshold": 14 }
assert my_solution.longestAlternatingSubarray(**test_input) == 2

test_input = { "nums": [3,1], "threshold": 9 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [3,4], "threshold": 10 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [3,10], "threshold": 3 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [4,2], "threshold": 11 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [4,2], "threshold": 15 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [4,4], "threshold": 9 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [4,7], "threshold": 8 }
assert my_solution.longestAlternatingSubarray(**test_input) == 2

test_input = { "nums": [4,9], "threshold": 17 }
assert my_solution.longestAlternatingSubarray(**test_input) == 2

test_input = { "nums": [5,8], "threshold": 5 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [5,8], "threshold": 15 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [6,2], "threshold": 10 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [6,4], "threshold": 14 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [6,4], "threshold": 16 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [6,5], "threshold": 10 }
assert my_solution.longestAlternatingSubarray(**test_input) == 2

test_input = { "nums": [7,3], "threshold": 8 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [7,4], "threshold": 7 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [7,5], "threshold": 1 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [7,10], "threshold": 52 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [7,17], "threshold": 31 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [8,4], "threshold": 6 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [8,8], "threshold": 20 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [9,2], "threshold": 11 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [9,4], "threshold": 15 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [10,3], "threshold": 11 }
assert my_solution.longestAlternatingSubarray(**test_input) == 2

test_input = { "nums": [10,4], "threshold": 7 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [10,5], "threshold": 20 }
assert my_solution.longestAlternatingSubarray(**test_input) == 2

test_input = { "nums": [10,7], "threshold": 11 }
assert my_solution.longestAlternatingSubarray(**test_input) == 2

test_input = { "nums": [10,8], "threshold": 4 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [10,18], "threshold": 43 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [12,34], "threshold": 7 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [12,35], "threshold": 8 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [13,9], "threshold": 53 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [15,13], "threshold": 23 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [15,15], "threshold": 18 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [17,2], "threshold": 17 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [23,37], "threshold": 35 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [24,11], "threshold": 54 }
assert my_solution.longestAlternatingSubarray(**test_input) == 2

test_input = { "nums": [27,9], "threshold": 55 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [27,17], "threshold": 40 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [33,4], "threshold": 43 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [41,16], "threshold": 9 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [47,44], "threshold": 20 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [49,39], "threshold": 52 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [50,8], "threshold": 19 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [76,46], "threshold": 91 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [1,1,7], "threshold": 4 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [1,3,1], "threshold": 18 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [1,4,3], "threshold": 1 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [1,5,3], "threshold": 8 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [1,10,5], "threshold": 9 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [2,1,8], "threshold": 6 }
assert my_solution.longestAlternatingSubarray(**test_input) == 2

test_input = { "nums": [2,10,5], "threshold": 7 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [3,2,8], "threshold": 18 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [3,3,10], "threshold": 20 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [3,4,2], "threshold": 19 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [3,6,10], "threshold": 6 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [3,8,9], "threshold": 19 }
assert my_solution.longestAlternatingSubarray(**test_input) == 2

test_input = { "nums": [4,3,1], "threshold": 4 }
assert my_solution.longestAlternatingSubarray(**test_input) == 2

test_input = { "nums": [4,4,4], "threshold": 8 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [4,5,10], "threshold": 3 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [4,10,2], "threshold": 4 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [4,10,3], "threshold": 8 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [4,10,3], "threshold": 10 }
assert my_solution.longestAlternatingSubarray(**test_input) == 2

test_input = { "nums": [4,40,8], "threshold": 45 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [5,3,9], "threshold": 7 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [5,5,6], "threshold": 7 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [6,2,2], "threshold": 16 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [6,2,4], "threshold": 17 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1

test_input = { "nums": [6,3,4], "threshold": 6 }
assert my_solution.longestAlternatingSubarray(**test_input) == 3

test_input = { "nums": [6,5,2], "threshold": 17 }
assert my_solution.longestAlternatingSubarray(**test_input) == 3

test_input = { "nums": [6,5,3], "threshold": 17 }
assert my_solution.longestAlternatingSubarray(**test_input) == 2

test_input = { "nums": [6,7,2], "threshold": 14 }
assert my_solution.longestAlternatingSubarray(**test_input) == 3

test_input = { "nums": [7,1,10], "threshold": 9 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0

test_input = { "nums": [7,5,6], "threshold": 8 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1