Skip to main content

Three Consecutive Odds - Solution & Explanation

EasyArray17 min readAsked at: Amazon, Meta, Google +2
Practice this problem

Problem Statement

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

 

Example 1:

Input: arr = [2,6,4,1]
Output: false
Explanation: There are no three consecutive odds.

Example 2:

Input: arr = [1,2,34,3,4,5,7,23,12]
Output: true
Explanation: [5,7,23] are three consecutive odds.

 

Constraints:

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

Approach Overview

Problem Overview: Given an integer array, determine whether there exist three numbers in a row that are all odd. You only need to detect a sequence of length three where every element satisfies num % 2 == 1. The array can be scanned once, making this a straightforward array traversal problem.

Approach 1: Iterative Counter (O(n) time, O(1) space)

The simplest approach tracks how many consecutive odd numbers you have seen while iterating through the array. For each element, check if it is odd using num % 2. If the number is odd, increment a counter; if it is even, reset the counter to zero because the streak breaks. The moment the counter reaches three, return true. This works because you only care about consecutive odds, so maintaining a running streak is enough.

This method performs a single pass through the array and stores only one integer counter, so the time complexity is O(n) and the space complexity is O(1). It is the most direct solution and usually what interviewers expect for this problem. The logic relies purely on sequential iteration, a common pattern in array problems.

Approach 2: Sliding Window of Size 3 (O(n) time, O(1) space)

Another way to model the problem is with a fixed-size sliding window. Maintain a window of three consecutive indices [i, i+1, i+2] and check whether all three numbers are odd. If they are, return true. Then slide the window one step to the right and repeat until the end of the array.

Each window check involves verifying three elements using the odd condition. Since the window moves across the array once, the total runtime remains O(n). Only a few variables are used for indexing, so the space complexity is O(1). While slightly more verbose than the counter approach, this method clearly demonstrates the fixed-window pattern that appears frequently in substring and subarray problems.

Recommended for interviews: The iterative counter approach is typically preferred. It shows that you recognize the problem as a simple streak-counting pattern and can implement it with minimal state. The sliding window approach is also valid and useful if you want to explicitly demonstrate familiarity with the fixed-size window technique. In practice, both achieve the same optimal complexity, but the counter solution is shorter and easier to reason about under interview pressure.

Approach 1: Iterative Approach

This approach involves iterating through the array and counting how many consecutive numbers are odd. If we reach a count of three consecutive odds, we return true immediately. If we finish scanning the array without finding three consecutive odd numbers, we return false.

This C solution iterates through the array with a simple loop. We maintain a count of how many consecutive elements are odd. If the count reaches three, we return true. Otherwise, we reset the count whenever an even number appears. This efficiently determines if there are three consecutive odd numbers.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the length of the array. Space Complexity: O(1) as we use only a constant amount of auxiliary space.

Try this approach in the editor →

Approach 2: Sliding Window Approach

A slightly different approach is using a sliding window of fixed size three. As we move the window along the array, we check if all numbers in the current window are odd, which allows us to efficiently determine if there are three consecutive odds.

This C solution implements a sliding window technique by checking non-overlapping windows of three elements to see if all elements are odd. This allows us to handle the problem in a clean and concise manner.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the length of the array since each element is traversed once. Space Complexity: O(1).

Try this approach in the editor →

Approach 3: Iteration + Counting

We use a variable cnt to record the current count of consecutive odd numbers.

Next, we iterate through the array. If the current element is odd, then cnt is incremented by one. If cnt equals 3, then return True. If the current element is even, then cnt is reset to zero.

After the iteration, if three consecutive odd numbers are not found, then return False.

The time complexity is O(n), where n is the length of the array arr. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Approach 4: Iteration + Bitwise Operation

Based on the properties of bitwise operations, the result of a bitwise AND operation between two numbers is odd if and only if both numbers are odd. If there are three consecutive numbers whose bitwise AND result is odd, then these three numbers are all odd.

Therefore, we only need to iterate through the array and check if there exists three consecutive numbers whose bitwise AND result is odd. If such numbers exist, return True; otherwise, return False.

The time complexity is O(n), where n is the length of the array arr. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Iterative Approach

Time Complexity: O(n), where n is the length of the array. Space Complexity: O(1) as we use only a constant amount of auxiliary space.

Sliding Window Approach

Time Complexity: O(n), where n is the length of the array since each element is traversed once. Space Complexity: O(1).

Iteration + Counting
Iteration + Bitwise Operation

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Iterative CounterO(n)O(1)Best general solution. Simple linear scan with a counter for consecutive odds.
Sliding Window (Size 3)O(n)O(1)Useful when practicing or demonstrating fixed-size sliding window patterns.

Video Solution

Three Consecutive Odds | Important Motivation | 2 Approaches | Leetcode 1550 | codestorywithMIKcodestorywithMIK2,576 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Three Consecutive Odds easy or hard?
Three Consecutive Odds is classified as an Easy problem on LeetCode. The task involves basic array traversal and a simple parity check using modulo. It is commonly recommended for beginners learning array iteration and streak-counting patterns.
How to solve Three Consecutive Odds in O(n)?
Iterate through the array and track the number of consecutive odd elements. For each number, check num % 2. If the number is odd, increase a running counter; if it is even, reset the counter to zero. The moment the counter reaches three, return true. If the loop finishes without reaching three, return false.
What is the best approach for Three Consecutive Odds?
The iterative counter approach is the best solution. Traverse the array once and maintain a counter for consecutive odd numbers. Increment the counter when an odd number appears and reset it when an even number appears. When the counter reaches three, return true. This runs in O(n) time and O(1) space.
What data structure is used in Three Consecutive Odds?
The problem primarily uses an array traversal. No additional data structures such as hash maps or stacks are required. A simple integer counter or a fixed-size sliding window over the array is enough to detect three consecutive odd numbers.
What is the time complexity of Three Consecutive Odds?
The optimal time complexity is O(n), where n is the length of the array. The algorithm performs a single pass through the array while checking whether each element is odd. Only constant extra space is used, so the space complexity is O(1).
Three Consecutive Odds Python or Java solution approach?
Both Python and Java implementations follow the same logic: iterate through the array and track consecutive odd numbers using a counter. When the counter reaches three, return true. This approach keeps the runtime O(n) and uses constant space.
Is Three Consecutive Odds asked at Google, Amazon, or Meta?
Three Consecutive Odds is categorized as an easy array problem and is more commonly used for screening rounds or practice rather than advanced interviews. Similar patterns—such as detecting consecutive elements or streaks—appear in interviews at companies like Amazon and Google.

Ready to solve this problem?

Practice Three Consecutive Odds with our built-in code editor and test cases.

Practice on FleetCode