File: three-consecutive-odds/solution.py

Date: 2026-06-06

Time: 19:29

three-consecutive-odds/solution.py

Purpose

Solves LeetCode 1550 — Three Consecutive Odds. Given an integer array, determine whether any three consecutive elements are all odd. This is a straightforward array-scanning problem classified as Easy.

Key Components

Solution.threeConsecutiveOdds(self, arr: List[int]) -> bool — The single method. It maintains a running count of consecutive odd numbers seen so far, returning True the moment that count hits 3, or False after exhausting the array.

Patterns

Streaming counter with reset. Rather than checking every triplet with a sliding window or triple-nested index comparison (arr[i] % 2 and arr[i+1] % 2 and arr[i+2] % 2), the solution uses a single counter that increments on odd and resets to 0 on even. This is a common idiom for "k consecutive elements matching a predicate" — it generalizes to any k by changing the threshold.

Early exit. The function returns True as soon as the condition is met, skipping the remainder of the array. Worst case is a full scan (O(n)), best case is O(1) if the first three elements are odd.

Dependencies

Flow

1. Initialize count = 0.

2. Iterate through each num in arr.

3. If num is odd (num % 2 == 1), increment count. If count reaches 3, return True immediately.

4. If num is even, reset count to 0.

5. If the loop completes without hitting 3, return False.

Invariants

Error Handling

None. The function trusts the caller to pass a valid list of integers within the stated constraints. No bounds checking, no empty-list guard — an empty list correctly returns False since the loop body never executes.

Topics to Explore

Beliefs