Three Consecutive Odds

The task requires us to determine if there are three consecutive odd numbers in the given array.

1
2
3
4
5
6
7
8
class Solution:
    def threeConsecutiveOdds(self, arr: List[int]) -> bool:
        # Iterate through the array, starting from index 0 to the third last element
        for i in range(len(arr) - 2):
            # Check if the current element and the next two elements are odd
            if arr[i] % 2 == 1 and arr[i + 1] % 2 == 1 and arr[i + 2] % 2 == 1:
                return True
        return False

Explanation

  • The code iterates through the array up to the third last element, as we need to check three consecutive numbers.
  • For each element, it checks if the current element and the next two elements are odd.
  • If it finds three consecutive odd numbers, it returns True. If not, it will continue iterating through the array.
  • If it does not find any three consecutive odd numbers in the entire array, it will return False.