Skip to main content
impol.dev

Editorial

Solution walkthrough

Review the explanation, then compare the reference implementation.

Video explanation

Solution

Intuition

Only the current uninterrupted run of odd values matters. An even number breaks every potential consecutive triple crossing its position, so it resets the run rather than merely being skipped.

Approach

Initialize a run counter to zero. Increase it for an odd value and reset it to zero for an even value. Return true as soon as the counter reaches three; any longer odd run necessarily reaches three first. Return false if the array ends without such a run. This checks adjacency, not the total number of odd values anywhere in the array.

class Solution: def threeConsecutiveOdds(self, arr: List[int]) -> bool: count = 0 for x in arr: if x % 2 == 1: count += 1 else: count = 0 if count == 3: return True return False

Complexity analysis

The running-count scan takes O(n) time and O(1) auxiliary space.