Skip to content
← All posts

Three Consecutive Odds: Simple Array Scanning

6 min read
leetcodeproblemeasyarrays

Given an array arr, return true if there are three consecutive odd numbers in the array. Otherwise, return false.

This is LeetCode 1550: Three Consecutive Odds, and it is a great entry point for learning the consecutive-element scanning pattern. The idea is simple: walk through the array, keep a running count of consecutive odd numbers, and return true the moment that count hits three.

206142133455
Blue = even numbers, orange = three consecutive odd numbers [1, 3, 5] found at indices 3, 4, 5.

Why this problem matters

Array scanning problems train you to think in terms of maintaining state as you iterate. Instead of checking every triplet with nested loops, you track a single counter that grows or resets based on each element. This "counter with reset" technique shows up in problems about consecutive characters, streaks in data, and many sliding window variants.

Once you internalize this pattern here, you can apply the same logic to problems like "Max Consecutive Ones" (LeetCode 485), "Longest Consecutive Sequence" (LeetCode 128), and any problem that asks you to detect a run of elements matching some condition.

The key insight

You do not need to check every group of three elements. You only need one variable: a counter that tracks how many consecutive odd numbers you have seen so far. When you see an odd number, increment the counter. When you see an even number, reset it to zero. If the counter ever reaches three, you are done.

This turns a potentially nested problem into a single pass through the array.

The algorithm:

  1. Initialize a counter to 0.
  2. For each element in the array, check if it is odd.
  3. If odd, increment the counter. If even, reset the counter to 0.
  4. If the counter reaches 3, return true.
  5. If you finish the loop without hitting 3, return false.

The solution

def three_consecutive_odds(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

Let's walk through what each piece does.

The count variable is the heart of the solution. It starts at zero and tracks how many odd numbers you have seen in an unbroken streak. Every time you encounter an odd number, you bump it up by one. Every time you see an even number, you slam it back to zero because the streak is broken.

The early return inside the if count == 3 check means you stop as soon as you find the answer. There is no reason to keep scanning after three consecutive odds are confirmed. In the best case, you find them near the start and return almost immediately.

The final return False handles the case where no streak of three odd numbers exists anywhere in the array.

You can also check oddness with num & 1 instead of num % 2 == 1. The bitwise AND is a common trick in interviews and works because the lowest bit of any odd number is always 1. Both approaches are correct, but the modulo version is easier to read.

Visual walkthrough

Let's trace through the array [2, 6, 4, 1, 3, 5] step by step. Watch how the counter resets on even numbers and grows on odd ones.

Index 0: value 2 is even. Count resets to 0.

206142133455

count = 0. No consecutive odds yet.

Index 1: value 6 is even. Count stays 0.

206142133455

count = 0. Still no odds in a row.

Index 2: value 4 is even. Count stays 0.

206142133455

count = 0. Three even numbers so far.

Index 3: value 1 is odd. Count becomes 1.

206142133455

count = 1. First odd in a potential streak.

Index 4: value 3 is odd. Count becomes 2.

206142133455

count = 2. Two consecutive odds.

Index 5: value 5 is odd. Count becomes 3. Found!

206142133455

count = 3. Three consecutive odds found. Return true.

Notice how the counter resets three times for the even numbers at the start, then steadily climbs once the odd streak begins. By the time we reach index 5, the counter hits 3 and we return true immediately.

Complexity analysis

ApproachTimeSpace
Counter scanO(n)O(1)

Time is O(n) where n is the length of the array. You visit each element at most once in a single pass. The early return means you often finish before scanning the entire array, but the worst case (no three consecutive odds) requires checking every element.

Space is O(1). You only use a single integer counter regardless of the input size. No extra arrays, hash maps, or data structures are needed.

The building blocks

1. Counting consecutive matches

The pattern of maintaining a counter that increments on a match and resets otherwise:

count = 0
for num in arr:
    if condition(num):
        count += 1
    else:
        count = 0

This is the same logic behind "Max Consecutive Ones," longest streak problems, and run-length encoding. The only thing that changes is the condition and the target count. You will reuse this building block any time you need to detect a streak of elements satisfying some property.

2. Early termination on threshold

The pattern of returning as soon as a counter hits a target value:

if count == target:
    return True

Checking inside the loop instead of after it lets you short-circuit the scan. This matters in large arrays where the answer might appear early. It also simplifies the logic because you do not need to track the maximum count, just whether the threshold was reached.

Edge cases

Before submitting, think through these scenarios:

  • Array shorter than 3 elements: impossible to have three consecutive anything. The loop finishes without the counter reaching 3, so you return false.
  • All even numbers: the counter never increments, staying at 0 the entire time. Return false.
  • All odd numbers: the counter hits 3 on the third element. Return true almost immediately.
  • Odds scattered but never three in a row: [1, 3, 2, 1, 3, 2] has pairs of odds but no triplet. The counter reaches 2 then resets each time. Return false.
  • Three odds at the very end: [2, 4, 6, 1, 3, 5] finds the answer on the last element. The early return triggers at index 5.
  • Single element array: [1] or [2]. The counter reaches at most 1. Return false.

From understanding to recall

You have seen how the counter-scan pattern works: initialize a counter, increment on odd, reset on even, return when you hit three. The code is about as short as it gets. But writing it correctly under pressure is a different story.

The details that trip people up are small but consequential. Do you reset the counter to 0 or to 1 when you see an even number? Do you check count == 3 before or after incrementing? Do you use num % 2 == 1 or num % 2 != 0 (they differ for negative numbers)? These are not conceptual gaps. They are recall gaps, and they slow you down when seconds matter.

Spaced repetition is how you close them. You practice writing the counter-scan loop from memory at increasing intervals. After a few rounds, the pattern is automatic. You see "consecutive elements matching a condition" in a problem description, and the counter template flows out without hesitation.

Related posts

CodeBricks breaks Three Consecutive Odds into its counter-scan and early-termination building blocks, then drills them independently with spaced repetition. You type each piece from scratch until the pattern is automatic. When a scanning problem shows up in your interview, you do not think about it. You just write it.