Skip to content

Commit 1340b56

Browse files
authored
Create longest-even-odd-subarray-with-threshold.cpp
1 parent 5da23ce commit 1340b56

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
// array
5+
class Solution {
6+
public:
7+
int longestAlternatingSubarray(vector<int>& nums, int threshold) {
8+
int result = 0, l = 0;
9+
for (const auto& x : nums) {
10+
if (x > threshold) {
11+
l = 0;
12+
continue;
13+
}
14+
if (l % 2 == x % 2) {
15+
++l;
16+
} else {
17+
l = (x + 1) % 2;
18+
}
19+
result = max(result, l);
20+
}
21+
return result;
22+
}
23+
};

0 commit comments

Comments
 (0)