Three Consecutive Odds - Problem
You're given an integer array arr and need to determine if there are three consecutive odd numbers anywhere in the array.
Goal: Return true if you can find three odd numbers that appear one after another in the array, otherwise return false.
What makes a number odd? A number is odd if it's not divisible by 2 (i.e., num % 2 == 1).
Example: In the array [2, 6, 4, 1, 3, 5, 8], we have three consecutive odd numbers: 1, 3, 5 at positions 3, 4, and 5, so we return true.
Input & Output
example_1.py โ Basic case with consecutive odds
$
Input:
[2, 6, 4, 1, 3, 5, 8]
โบ
Output:
true
๐ก Note:
The array contains three consecutive odd numbers: 1, 3, 5 at indices 3, 4, and 5.
example_2.py โ No three consecutive odds
$
Input:
[1, 2, 34, 3, 6, 7, 23, 12]
โบ
Output:
false
๐ก Note:
While there are odd numbers (1, 3, 7, 23), no three of them appear consecutively.
example_3.py โ All odd numbers
$
Input:
[1, 3, 5]
โบ
Output:
true
๐ก Note:
The entire array consists of three consecutive odd numbers.
Visualization
Tap to expand
Understanding the Visualization
1
Start monitoring
Begin walking down the street, counting consecutive green lights
2
Count greens
For each green light, increment counter; for red lights, reset to 0
3
Found pattern
When we see 3 consecutive greens, we've found our answer!
Key Takeaway
๐ฏ Key Insight: We only need to track consecutive odd numbers with a simple counter, resetting when we hit even numbers and returning true immediately when the counter reaches 3!
Time & Space Complexity
Time Complexity
O(n)
Single pass through the array, visiting each element exactly once
โ Linear Growth
Space Complexity
O(1)
Only using one integer variable to count consecutive odds
โ Linear Space
Constraints
- 1 โค arr.length โค 1000
- 1 โค arr[i] โค 1000
- Array elements can be any positive integers
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code