Three Consecutive Odds (LeetCode 1550): Solution and Explanation
Return true if an integer array has three odd numbers at adjacent positions, and false otherwise.
Intuition
Three consecutive odds exist exactly when some run of odd numbers reaches length 3. So the only thing worth remembering while scanning is the length of the run that ends at the current position.
An odd number extends that run by one. An even number ends it, and any later triple must start after the even number, so the run length drops to 0.
The moment the counter hits 3 the answer is known, so the scan can stop early.
Approach: Run counter
- Set
streak = 0. - For each
numinarr: ifnumis odd, incrementstreakand returntrueonce it equals 3. - If
numis even, setstreak = 0. - After the loop, return
false.
Step-by-step example
arr = [1,2,34,3,4,5,7,23,12]
- 1 is odd, streak = 1. 2 is even, streak = 0. 34 is even, streak stays 0.
- 3 is odd, streak = 1. 4 is even, streak = 0.
- 5 is odd, streak = 1. 7 is odd, streak = 2.
- 23 is odd, streak = 3: return true without looking at 12.
Code: Python, Java, C++
1class Solution:
2 def threeConsecutiveOdds(self, arr: List[int]) -> bool:
3 streak = 0
4 for num in arr:
5 if num % 2 == 1:
6 streak += 1
7 if streak == 3:
8 return True
9 else:
10 streak = 0
11 return False1class Solution {
2 public boolean threeConsecutiveOdds(int[] arr) {
3 int streak = 0;
4 for (int num : arr) {
5 if (num % 2 == 1) {
6 streak++;
7 if (streak == 3) {
8 return true;
9 }
10 } else {
11 streak = 0;
12 }
13 }
14 return false;
15 }
16}1class Solution {
2public:
3 bool threeConsecutiveOdds(vector<int>& arr) {
4 int streak = 0;
5 for (int num : arr) {
6 if (num % 2 == 1) {
7 streak++;
8 if (streak == 3) {
9 return true;
10 }
11 } else {
12 streak = 0;
13 }
14 }
15 return false;
16 }
17};Time and space complexity
O(n)
Each element is read at most once.
O(1)
Only the counter is stored.
Other approaches
Check every window of three (time O(n), space O(1))
For each i from 0 to n - 3, test whether arr[i], arr[i+1] and arr[i+2] are all odd. Equally fast and just as simple; it reads each element up to three times.
Product parity (time O(n), space O(1))
A product of three integers is odd only when all three are odd, so test arr[i] * arr[i+1] * arr[i+2] % 2 == 1 for each window. A cute trick, but the run counter is clearer.
Common mistakes
- Counting odd numbers in total instead of consecutive ones. [1,2,3,4,5] has three odds but no three in a row.
- Forgetting to reset the counter on an even number.
- Windowed solutions that index arr[i+2] past the end when the array has fewer than three elements.
- Testing oddness with num % 2 == 1 in languages where negative numbers give -1. The constraints make values positive here, but num % 2 != 0 is the safer habit.
Frequently asked questions
Why can the counter drop straight to 0 on an even number?
A triple of consecutive odds cannot include an even number, so any triple must lie entirely after it. The run before the even number can never be part of an answer.
Does the answer change if there are more than three odds in a row?
No. A run of four or more contains a run of three, and the scan returns true as soon as the third one arrives.
What if the array has fewer than three elements?
The counter can never reach 3, so the loop ends and the function returns false.