Watch 10 video solutions for Three Consecutive Odds, a easy level problem involving Array. This walkthrough by codestorywithMIK has 2,491 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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. |