Difficulty: Easy | Acceptance: 69.30% | Paid: No Topics: Array
Given an integer array arr, return true if there are three consecutive odd numbers in the array. Otherwise, return false.
- Examples
- Constraints
- Linear Scan
- Sliding Window Check
- Functional Approach
Examples
Input: arr = [2,6,4,1]
Output: false
Explanation: There are no three consecutive odds.
Input: arr = [1,2,34,3,4,5,7,23,12]
Output: true
Explanation: Three consecutive odds at indices 5, 6, and 7 are [5,7,23].
Constraints
1 <= arr.length <= 1000
1 <= arr[i] <= 1000
Linear Scan
Intuition We iterate through the array while maintaining a counter of how many consecutive odd numbers we have seen so far. If the counter reaches 3, we return true immediately. If we encounter an even number, we reset the counter to 0.
Steps
- Initialize a counter
countto 0. - Iterate through each number in the array.
- If the number is odd (num % 2 != 0), increment
count. - If
countreaches 3, return true. - If the number is even, reset
countto 0. - If the loop finishes without returning true, return false.
class Solution:
def threeConsecutiveOdds(self, arr: list[int]) -> bool:
count = 0
for num in arr:
if num % 2 != 0:
count += 1
if count == 3:
return True
else:
count = 0
return False
Complexity
- Time: O(n) — We traverse the array once.
- Space: O(1) — We only use a single counter variable.
- Notes: This is the most efficient approach as it stops as soon as the condition is met.
Sliding Window Check
Intuition We can iterate through the array and explicitly check every window of size 3. If we find a window where all three elements are odd, we return true.
Steps
- Iterate from index
i = 0toarr.length - 3. - For each
i, check ifarr[i],arr[i+1], andarr[i+2]are all odd. - If the condition is met, return true.
- If the loop completes, return false.
class Solution:
def threeConsecutiveOdds(self, arr: list[int]) -> bool:
for i in range(len(arr) - 2):
if arr[i] % 2 != 0 and arr[i+1] % 2 != 0 and arr[i+2] % 2 != 0:
return True
return False
Complexity
- Time: O(n) — We iterate up to n-3 times.
- Space: O(1) — No extra space is used.
- Notes: Slightly less efficient than the linear scan with a counter if the array is very large and the odds are at the very end, but effectively O(n).
Functional Approach
Intuition
We utilize built-in functional methods like some in JavaScript or any in Python to check for the existence of a valid window without writing an explicit loop structure.
Steps
- Use the array’s iteration method to check if there exists an index
isuch thati,i+1, andi+2are all odd. - Return the result of this check.
class Solution:
def threeConsecutiveOdds(self, arr: list[int]) -> bool:
return any(arr[i] % 2 != 0 and arr[i+1] % 2 != 0 and arr[i+2] % 2 != 0 for i in range(len(arr) - 2))
Complexity
- Time: O(n) — The underlying iteration is still linear.
- Space: O(1) — No significant additional memory is used.
- Notes: This approach offers cleaner, more declarative code but may have slight overhead compared to a raw loop.