#1550

Three Consecutive Odds

Easy
ArrayArrayTwo Pointers
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(n)
Space
O(1)
O(1)
💡

Intuition

Time O(n)Space O(1)

This approach uses a single loop to track consecutive odd numbers, making it much more efficient. We only need to check each number once.

⚙️

Algorithm

4 steps
  1. 1Step 1: Initialize a counter to track consecutive odd numbers.
  2. 2Step 2: Loop through the array and check if each number is odd.
  3. 3Step 3: If it is odd, increment the counter. If it is even, reset the counter to zero.
  4. 4Step 4: If the counter reaches 3 at any point, return true. If the loop ends without reaching 3, return false.
solution.py10 lines
1def threeConsecutiveOdds(arr):
2    count = 0
3    for num in arr:
4        if num % 2 != 0:
5            count += 1
6            if count == 3:
7                return True
8        else:
9            count = 0
10    return False

Complexity note: The time complexity is O(n) because we only loop through the array once. The space complexity is O(1) since we use a constant amount of extra space.

  • 1Consecutive elements can be tracked using a counter.
  • 2Checking parity (odd/even) is a simple modulus operation.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.