Problem
Given an integer array arr
, return true
if there are three consecutive odd numbers in the array. Otherwise, return false
.
Examples
Example 1:
|
|
Example 2:
|
|
Solution
Method 1 - Simply count and iteration
We can follow following steps:
- Iterates over the array,
- maintaining a counter
cnt
- Reset to
cnt
to 0 whenever an even number is encountered. - If an odd number is found, the counter is incremented.
- If the counter reaches 3, indicating three consecutive odd numbers, the method returns
true
.
- If the counter reaches 3, indicating three consecutive odd numbers, the method returns
- maintaining a counter
- If the loop completes without finding such a sequence, the method returns
false
.
Here is the video explanation:
Code
|
|
Complexity
- ⏰ Time complexity:
O(n)
- 🧺 Space complexity:
O(1)