Three Consecutive Odds solution and explanation
9 more videos on this problem
Three Consecutive Odds | Important Motivation | 2 Approaches | Leetcode 1550 | codestorywithMIKcodestorywithMIK · 9:58 · 2K views
Leetcode Solution - 1550 Three Consecutive OddsJohn Leonardo · 2:53 · 714 views
1550. Three Consecutive Odds (Leetcode Easy)Programming Live with Larry · 1:30 · 452 views
THREE CONSECUTIVE ODDS | LEETCODE 1550 | PYTHON SOLUTIONCracking FAANG · 3:43 · 301 views
How to Solve "1550 Three Consecutive Odds" on LeetCode? - JavascriptBecoming A Better Dev (with David) · 4:08 · 234 views
Three Consecutive Odds - Leetcode 1550 - PythonTim Huang · 2:10 · 164 views
1550. Three Consecutive Odds | Leetcode Daily - PythonLeetcode Daily · 6:24 · 157 views
Leetcode 1550.Three Consecutive OddsQuasar · 1:16 · 121 views
LeetCode Daily Question | 1550. Three Consecutive Odds | Solution | Code explained.Pavan Logics · 5:44 · 101 views
Leetcode solving : 1550. Three Consecutive OddsMath Geeks · 1:44 · 95 views
The problem
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] <= 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.
def threeConsecutiveOdds(arr):
count = 0
for num in arr:
if num % 2 != 0:
count += 1
if count == 3:
return True
else:
count = 0
return Falseclass Solution {
public boolean threeConsecutiveOdds(int[] arr) {
int count = 0;
for (int num : arr) {
if (num % 2 != 0) {
count++;
if (count == 3) return true;
} else {
count = 0;
}
}
return false;
}
}#include <vector>
using namespace std;
bool threeConsecutiveOdds(vector<int>& arr) {
int count = 0;
for (int num : arr) {
if (num % 2 != 0) {
count++;
if (count == 3) return true;
} else {
count = 0;
}
}
return false;
}function threeConsecutiveOdds(arr) {
let count = 0;
for (let num of arr) {
if (num % 2 !== 0) {
count++;
if (count === 3) return true;
} else {
count = 0;
}
}
return false;
}#include <stdbool.h>
bool threeConsecutiveOdds(int* arr, int arrSize) {
int count = 0;
for (int i = 0; i < arrSize; i++) {
if (arr[i] % 2 != 0) {
count++;
if (count == 3) return true;
} else {
count = 0;
}
}
return false;
}public class Solution {
public bool ThreeConsecutiveOdds(int[] arr) {
int count = 0;
foreach (int num in arr) {
if (num % 2 != 0) {
count++;
if (count == 3) return true;
} else {
count = 0;
}
}
return false;
}
}The Python solution uses a simple for loop to iterate over the list. We check each element to see if it is odd, incrementing our count of consecutive odds. If an even is found, we reset the counter. We return true as soon as three consecutive odds are found.
Time Complexity: O(n), where n is the length of the list. Space Complexity: O(1).
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.
def threeConsecutiveOdds(arr):
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 Falseclass Solution {
public boolean threeConsecutiveOdds(int[] arr) {
if (arr.length < 3) return false;
for (int i = 0; i <= arr.length - 3; i++) {
if (arr[i] % 2 != 0 && arr[i+1] % 2 != 0 && arr[i+2] % 2 != 0) {
return true;
}
}
return false;
}
}#include <vector>
using namespace std;
bool threeConsecutiveOdds(vector<int>& arr) {
for (int i = 0; i <= arr.size() - 3; i++) {
if (arr[i] % 2 != 0 && arr[i+1] % 2 != 0 && arr[i+2] % 2 != 0) {
return true;
}
}
return false;
}function threeConsecutiveOdds(arr) {
for (let i = 0; i <= arr.length - 3; i++) {
if (arr[i] % 2 !== 0 && arr[i+1] % 2 !== 0 && arr[i+2] % 2 !== 0) {
return true;
}
}
return false;
}#include <stdbool.h>
bool threeConsecutiveOdds(int* arr, int arrSize) {
if (arrSize < 3) return false;
for (int i = 0; i <= arrSize - 3; i++) {
if (arr[i] % 2 != 0 && arr[i+1] % 2 != 0 && arr[i+2] % 2 != 0) {
return true;
}
}
return false;
}public class Solution {
public bool ThreeConsecutiveOdds(int[] arr) {
for (int i = 0; i <= arr.Length - 3; i++) {
if (arr[i] % 2 != 0 && arr[i+1] % 2 != 0 && arr[i+2] % 2 != 0) {
return true;
}
}
return false;
}
}In Python, this approach involves checking each possible triplet (or window of size three) as we move through the array, returning true if we hit a triplet of odd numbers.
Time Complexity: O(n), Space Complexity: O(1).
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).
function threeConsecutiveOdds(arr: number[]): boolean {
let cnt = 0;
for (const x of arr) {
if (x & 1) {
if (++cnt == 3) {
return true;
}
} else {
cnt = 0;
}
}
return false;
}func threeConsecutiveOdds(arr []int) bool {
cnt := 0
for _, x := range arr {
if x&1 == 1 {
cnt++
if cnt == 3 {
return true
}
} else {
cnt = 0
}
}
return false
}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).
function threeConsecutiveOdds(arr: number[]): boolean {
const n = arr.length;
for (let i = 2; i < n; ++i) {
if (arr[i - 2] & arr[i - 1] & arr[i] & 1) {
return true;
}
}
return false;
}func threeConsecutiveOdds(arr []int) bool {
for i, n := 2, len(arr); i < n; i++ {
if arr[i-2]&arr[i-1]&arr[i]&1 == 1 {
return true
}
}
return false
}Complexity comparison
| Approach | Complexity |
|---|---|
| Iterative Approach | Time Complexity: O(n), where n is the length of the list. Space Complexity: O(1). |
| Sliding Window Approach | Time Complexity: O(n), Space Complexity: O(1). |
| Iteration + Counting | – |
| Iteration + Bitwise Operation | – |
Complexity analysis
| 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. |
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.
