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.
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 3is the maximum possible achievable length.
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 1is the maximum possible achievable length.
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 3is the maximum possible achievable length.
We want the longest subarray starting with an even number, alternating even/odd, and all elements ≤ threshold. We can use a sliding window to expand as long as the conditions hold, and reset when broken.
classSolution {
public:int longestAlternatingSubarray(vector<int>& nums, int threshold) {
int n = nums.size(), ans =0;
for (int i =0; i < n; ++i) {
if (nums[i] %2==0&& nums[i] <= threshold) {
int j = i, cur =0, last = nums[i] %2;
while (j < n && nums[j] %2== last && nums[j] <= threshold) {
cur++;
last ^=1;
j++;
}
ans = max(ans, cur);
}
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
funclongestAlternatingSubarray(nums []int, thresholdint) int {
n, ans:= len(nums), 0fori:=0; i < n; i++ {
ifnums[i]%2==0&&nums[i] <=threshold {
j, cur, last:=i, 0, nums[i]%2forj < n&&nums[j]%2==last&&nums[j] <=threshold {
cur++last ^= 1j++ }
ifcur > ans {
ans = cur }
}
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
publicintlongestAlternatingSubarray(int[] nums, int threshold) {
int n = nums.length, ans = 0;
for (int i = 0; i < n; i++) {
if (nums[i]% 2 == 0 && nums[i]<= threshold) {
int j = i, cur = 0, last = nums[i]% 2;
while (j < n && nums[j]% 2 == last && nums[j]<= threshold) {
cur++;
last ^= 1;
j++;
}
ans = Math.max(ans, cur);
}
}
return ans;
}
}
classSolution {
funlongestAlternatingSubarray(nums: IntArray, threshold: Int): Int {
val n = nums.size
var ans = 0for (i in0 until n) {
if (nums[i] % 2==0&& nums[i] <= threshold) {
var j = i
var cur = 0var last = nums[i] % 2while (j < n && nums[j] % 2== last && nums[j] <= threshold) {
cur++ last = last xor 1 j++ }
ans = maxOf(ans, cur)
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution:
deflongestAlternatingSubarray(self, nums: list[int], threshold: int) -> int:
n = len(nums)
ans =0 i =0while i < n:
if nums[i] %2==0and nums[i] <= threshold:
j = i
last = nums[i] %2 cur =0while j < n and nums[j] %2== last and nums[j] <= threshold:
cur +=1 last ^=1 j +=1 ans = max(ans, cur)
i +=1return ans
impl Solution {
pubfnlongest_alternating_subarray(nums: Vec<i32>, threshold: i32) -> i32 {
let n = nums.len();
letmut ans =0;
letmut i =0;
while i < n {
if nums[i] %2==0&& nums[i] <= threshold {
letmut j = i;
letmut last = nums[i] %2;
letmut cur =0;
while j < n && nums[j] %2== last && nums[j] <= threshold {
cur +=1;
last ^=1;
j +=1;
}
ans = ans.max(cur);
}
i +=1;
}
ans
}
}