EasyNo. 1550

Three Consecutive Odds — LeetCode 1550 Python Solution

Topics
Array
Reading time
2 min

The problem

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

Example

Input
arr = [2,6,4,1]
Output
false
Explanation
There are no three consecutive odds.

Python solution

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

Complexity

Time
O(n)where n is the length of the array arr
Space · auxiliary
O(1)

Related problems

Related problems
No.ProblemDifficulty
0057Insert IntervalMedium
0228Summary RangesEasy
0485Max Consecutive OnesEasy
0665Non-decreasing ArrayMedium
0674Longest Continuous Increasing SubsequenceEasy
0699Falling SquaresHard

Frequently asked questions

How hard is LeetCode 1550. Three Consecutive Odds?

LeetCode 1550. Three Consecutive Odds is rated Easy on LeetCode.

What is the time complexity of LeetCode 1550. Three Consecutive Odds?

The Python solution on this page runs in O(n), where n is the length of the array \textit{arr}.

What is the space complexity of LeetCode 1550. Three Consecutive Odds?

The Python solution on this page uses O(1) auxiliary space.

What topics does LeetCode 1550. Three Consecutive Odds cover?

LeetCode 1550. Three Consecutive Odds is tagged Array on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.