Three Consecutive Odds
EasyPrompt
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
Approaches
2 approaches with complexity analysis and trade-offs.
This approach involves iterating through the array and checking every possible contiguous window of size three. For each window, we verify if all three numbers are odd.
Algorithm
- Check if the array has fewer than 3 elements. If so, return
falseas it's impossible to have three consecutive numbers. - Loop through the array from index
i = 0toarr.length - 3. - Inside the loop, check if
arr[i],arr[i+1], andarr[i+2]are all odd using the modulo operator (%). - If all three are odd, we have found our sequence, so we return
true. - If the loop finishes without finding such a sequence, we return
false.
Walkthrough
We can solve this by iterating through the array with a loop. The loop should start at index 0 and end at arr.length - 3 to ensure there are at least three elements left to check (the current one, and the next two).
In each iteration, for the current index i, we check if the number arr[i], the next number arr[i+1], and the number after that arr[i+2] are all odd.
An integer x is odd if the remainder of its division by 2 is not 0 (i.e., x % 2 != 0).
If we find a triplet (arr[i], arr[i+1], arr[i+2]) where all three are odd, we can immediately stop and return true.
If the loop finishes without finding any such triplet, it means no three consecutive odd numbers exist in the array, so we return false.
class Solution { public boolean threeConsecutiveOdds(int[] arr) { // We need at least 3 elements to have 3 consecutive odds. if (arr.length < 3) { return false; } // Iterate up to the third-to-last element. for (int i = 0; i <= arr.length - 3; i++) { // Check if the current, next, and next-next elements are all odd. if (arr[i] % 2 != 0 && arr[i+1] % 2 != 0 && arr[i+2] % 2 != 0) { return true; } } // If the loop completes, no such sequence was found. return false; }}Complexity
Time
O(n), where `n` is the length of the array. We iterate through the array approximately `n-2` times, and each check is a constant time operation.
Space
O(1), as we don't use any extra space that scales with the input size.
Trade-offs
Pros
Simple to understand and implement.
Cons
In each step of the loop, we might re-check elements that were already checked in the previous step. For example,
arr[i+1]andarr[i+2]in one iteration will be checked again asarr[i]andarr[i+1]in the next iteration.
A more optimized approach is to iterate through the array just once while keeping a count of consecutive odd numbers encountered so far. This avoids re-checking elements.
Algorithm
- Initialize a counter
oddCountto 0. - Loop through each number
numin the arrayarr. - If
numis odd, incrementoddCount. - If
numis even, resetoddCountto 0. - In each iteration, after updating the counter, check if
oddCountis equal to 3. If it is, returntrue. - If the loop completes, return
false.
Walkthrough
This approach uses a single pass and a counter to track the number of consecutive odd numbers. This is more efficient as it avoids re-inspecting elements.
We initialize a counter, say oddCount, to 0. We then iterate through each number in the array arr.
For each number:
- If the number is odd, we increment
oddCount. - If the number is even, it breaks any existing sequence of odds, so we must reset
oddCountto 0.
After processing each number, we check if oddCount has reached 3. If it has, we've found our target sequence and can return true immediately.
If we finish iterating through the entire array and the counter never reached 3, it means no such sequence exists, and we return false.
class Solution { public boolean threeConsecutiveOdds(int[] arr) { int oddCount = 0; for (int num : arr) { if (num % 2 != 0) { oddCount++; } else { // Reset the counter if we find an even number. oddCount = 0; } // Check if we have found three consecutive odds. if (oddCount == 3) { return true; } } // If we finish the loop, it means we didn't find 3 consecutive odds. return false; }}Complexity
Time
O(n), where `n` is the length of the array. We traverse the array a single time.
Space
O(1), as we only use a single integer variable for the counter.
Trade-offs
Pros
Highly efficient as it processes each element only once.
Easily generalizable to finding 'k' consecutive odds.
Cons
No significant cons for this problem; it's an optimal solution.
Solutions
Solution
class Solution {public boolean threeConsecutiveOdds(int[] arr) { int cnt = 0; for (int v : arr) { if (v % 2 == 1) { ++cnt; } else { cnt = 0; } if (cnt == 3) { return true; } } return false; }}class Solution {public: bool threeConsecutiveOdds(vector<int> &arr) { int cnt = 0; for (int v : arr) { if (v & 1) ++cnt; else cnt = 0; if (cnt == 3) return true; } return false; }};class Solution: def threeConsecutiveOdds(self, arr: List[int]) -> bool: cnt = 0 for v in arr: if v & 1: cnt += 1 else: cnt = 0 if cnt == 3: return True return FalseVideo walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.