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 <= 10001 <= arr[i] <= 1000Problem 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.
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.
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.
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.
Time Complexity: O(n), where n is the length of the array since each element is traversed once. Space Complexity: O(1).
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).
Python
Java
C++
Go
TypeScript
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).
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| 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 | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Counter | O(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. |
Three Consecutive Odds | Important Motivation | 2 Approaches | Leetcode 1550 | codestorywithMIK • codestorywithMIK • 2,491 views views
Watch 9 more video solutions →Practice Three Consecutive Odds with our built-in code editor and test cases.
Practice on FleetCode