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
๐Ÿšฆ Traffic Light Pattern DetectiveRed = Even, Green = Odd | Find 3 consecutive greens2641358๐ŸŽฏ Found: 3 Consecutive Greens!Count: 0Count: 0Count: 0Count: 1Count: 2Count: 3 โœ“-๐Ÿ” Detection Algorithmโ€ข Walk through each traffic lightโ€ข ๐Ÿ”ด Red light (even) โ†’ Reset counter to 0โ€ข ๐ŸŸข Green light (odd) โ†’ Increment counterโ€ข ๐ŸŽฏ Counter reaches 3 โ†’ Pattern found!โœ… Result: true (found at lights 4-5-6)
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

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using one integer variable to count consecutive odds

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค arr.length โ‰ค 1000
  • 1 โ‰ค arr[i] โ‰ค 1000
  • Array elements can be any positive integers
Asked in
Amazon 25 Google 18 Microsoft 15 Meta 12
42.0K Views
Medium Frequency
~12 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen