Back to blog
Sep 13, 2025
3 min read

Three Consecutive Odds

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

Difficulty: Easy | Acceptance: 69.30% | Paid: No Topics: Array

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

Examples

Input: arr = [2,6,4,1]
Output: false
Explanation: There are no three consecutive odds.
Input: arr = [1,2,34,3,4,5,7,23,12]
Output: true
Explanation: Three consecutive odds at indices 5, 6, and 7 are [5,7,23].

Constraints

1 <= arr.length <= 1000
1 <= arr[i] <= 1000

Linear Scan

Intuition We iterate through the array while maintaining a counter of how many consecutive odd numbers we have seen so far. If the counter reaches 3, we return true immediately. If we encounter an even number, we reset the counter to 0.

Steps

  • Initialize a counter count to 0.
  • Iterate through each number in the array.
  • If the number is odd (num % 2 != 0), increment count.
  • If count reaches 3, return true.
  • If the number is even, reset count to 0.
  • If the loop finishes without returning true, return false.
python
class Solution:
    def threeConsecutiveOdds(self, arr: list[int]) -&gt; bool:
        count = 0
        for num in arr:
            if num % 2 != 0:
                count += 1
                if count == 3:
                    return True
            else:
                count = 0
        return False

Complexity

  • Time: O(n) — We traverse the array once.
  • Space: O(1) — We only use a single counter variable.
  • Notes: This is the most efficient approach as it stops as soon as the condition is met.

Sliding Window Check

Intuition We can iterate through the array and explicitly check every window of size 3. If we find a window where all three elements are odd, we return true.

Steps

  • Iterate from index i = 0 to arr.length - 3.
  • For each i, check if arr[i], arr[i+1], and arr[i+2] are all odd.
  • If the condition is met, return true.
  • If the loop completes, return false.
python
class Solution:
    def threeConsecutiveOdds(self, arr: list[int]) -&gt; bool:
        for i in range(len(arr) - 2):
            if arr[i] % 2 != 0 and arr[i+1] % 2 != 0 and arr[i+2] % 2 != 0:
                return True
        return False

Complexity

  • Time: O(n) — We iterate up to n-3 times.
  • Space: O(1) — No extra space is used.
  • Notes: Slightly less efficient than the linear scan with a counter if the array is very large and the odds are at the very end, but effectively O(n).

Functional Approach

Intuition We utilize built-in functional methods like some in JavaScript or any in Python to check for the existence of a valid window without writing an explicit loop structure.

Steps

  • Use the array’s iteration method to check if there exists an index i such that i, i+1, and i+2 are all odd.
  • Return the result of this check.
python
class Solution:
    def threeConsecutiveOdds(self, arr: list[int]) -&gt; bool:
        return any(arr[i] % 2 != 0 and arr[i+1] % 2 != 0 and arr[i+2] % 2 != 0 for i in range(len(arr) - 2))

Complexity

  • Time: O(n) — The underlying iteration is still linear.
  • Space: O(1) — No significant additional memory is used.
  • Notes: This approach offers cleaner, more declarative code but may have slight overhead compared to a raw loop.