1550: Three-Consecutive-Odds
Easy
table of contents
Basically, just iterate through the array arr,
and keep track of a count:
Then, after we check the parity of the element, we can
simply return true if count == 3. Else, after
all the iterations, we can simply just return false (since
we never have 3 consecutive odd numbers).
code
class Solution {
public:
bool threeConsecutiveOdds(vector<int>& arr) {
int count = 0;
for (int& i : arr) {
if (i % 2 == 1) {
++count;
} else {
count = 0;
}
if (count == 3) {
return true;
}
}
return false;
}
};