Skip to content

← leetcode

Three Consecutive Odds

· Easy

LeetCode problem 1550

Basically, just iterate through the array arr, and keep track of a count:

  • if arr[i] is even, then we can set count = 0
  • if arr[i] is odd, then we can increment 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;
    }
};

complexity

  • Time: O(n) where n is the size of arr
  • Space: O(1)

time taken

  • like less than a minut-