1550: Three-Consecutive-Odds
Easy
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 {
= 0;
count }
if (count == 3) {
return true;
}
}
return false;
}
};