I iterate through the array while maintaining a counter that tracks consecutive odd numbers. Each time I encounter an odd number, I increment the counter. If the counter reaches 3, I immediately return True. If I encounter an even number, I reset the counter to 0.
class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
count = 0
for num in arr:
if num % 2 == 1:
count += 1
if count == 3:
return True
else:
count = 0
return False
- Time: O(n)
- Space: O(1)
