Skip to content

1550. Three Consecutive Odds 👍

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
 public:
  bool threeConsecutiveOdds(vector<int>& arr) {
    int count = 0;
    for (const int a : arr) {
      count = a % 2 == 0 ? 0 : count + 1;
      if (count == 3)
        return true;
    }
    return false;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
  public boolean threeConsecutiveOdds(int[] arr) {
    int count = 0;
    for (final int a : arr) {
      count = a % 2 == 0 ? 0 : count + 1;
      if (count == 3)
        return true;
    }
    return false;
  }
}
1
2
3
4
5
6
7
8
class Solution:
  def threeConsecutiveOdds(self, arr: list[int]) -> bool:
    count = 0
    for a in arr:
      count = 0 if a % 2 == 0 else count + 1
      if count == 3:
        return True
    return False