2760. Longest Even Odd Subarray With Threshold Easy

@problem@discussion
#Array#Sliding Window



1/**
2 * [2760] Longest Even Odd Subarray With Threshold
3 *
4 * You are given a 0-indexed integer array nums and an integer threshold.
5 * 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:
6 * 
7 * 	nums[l] % 2 == 0
8 * 	For all indices i in the range [l, r - 1], nums[i] % 2 != nums[i + 1] % 2
9 * 	For all indices i in the range [l, r], nums[i] <= threshold
10 * 
11 * Return an integer denoting the length of the longest such subarray.
12 * Note: A subarray is a contiguous non-empty sequence of elements within an array.
13 *  
14 * <strong class="example">Example 1:
15 * 
16 * Input: nums = [3,2,5,4], threshold = 5
17 * Output: 3
18 * 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.
19 * Hence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length.
20 * <strong class="example">Example 2:
21 * 
22 * Input: nums = [1,2], threshold = 2
23 * Output: 1
24 * Explanation: In this example, we can select the subarray that starts at l = 1 and ends at r = 1 => [2]. 
25 * It satisfies all the conditions and we can show that 1 is the maximum possible achievable length.
26 * 
27 * <strong class="example">Example 3:
28 * 
29 * Input: nums = [2,3,4,5], threshold = 4
30 * Output: 3
31 * Explanation: In this example, we can select the subarray that starts at l = 0 and ends at r = 2 => [2,3,4]. 
32 * It satisfies all the conditions.
33 * Hence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length.
34 * 
35 *  
36 * Constraints:
37 * 
38 * 	1 <= nums.length <= 100 
39 * 	1 <= nums[i] <= 100 
40 * 	1 <= threshold <= 100
41 * 
42 */
43pub struct Solution {}
44
45// problem: https://leetcode.com/problems/longest-even-odd-subarray-with-threshold/
46// discuss: https://leetcode.com/problems/longest-even-odd-subarray-with-threshold/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50impl Solution {
51    pub fn longest_alternating_subarray(nums: Vec<i32>, threshold: i32) -> i32 {
52        0
53    }
54}
55
56// submission codes end
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn test_2760() {
64    }
65}
66


Back
© 2025 bowen.ge All Rights Reserved.