Basically, just iterate through the array arr, and keep track of a count:
- if
arr[i]is even, then we can setcount = 0- if
arr[i]is odd, then we can incrementcount
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;
}
};
complexity
- Time:
O(n)wherenis the size ofarr- Space:
O(1)
time taken
- like less than a minut-