[LeetCode] Three Consecutive Odds

1550. Three Consecutive Odds

Given an integer array arr, return true if there are three consecutive odd numbers in the array. Otherwise, return false.

1
2
3
4
5
6
7
8
9
10
class Solution {
public:
bool threeConsecutiveOdds(vector<int>& arr) {
for(int i = 0, bit = 0, mask = 7; i < arr.size(); i++, bit = (bit * 2) & mask) {
bit ^= (arr[i] & 1);
if(bit == mask) return true;
}
return false;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/05/17/PS/LeetCode/three-consecutive-odds/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.