The "Three Consecutive Odds" problem asks you to determine whether a given list of integers contains any three consecutive numbers that are all odd. Given an array arr
, your task is to return True
if there exists at least one set of three adjacent elements in arr
such that all three are odd numbers. Otherwise, return False
.
arr
is an integer.i
, i+1
, and i+2
).True
as soon as you find such a triplet; otherwise, return False
after checking the entire array.
To solve this problem, let's first consider a straightforward approach: for every group of three consecutive elements in the array, check if all of them are odd. If any such group exists, we can immediately return True
. If we reach the end of the array without finding such a group, we return False
.
The brute-force method would involve iterating through every possible triplet of consecutive elements and checking their parity. However, since we only need to check consecutive elements, we can do this efficiently with a single loop.
There's no need for extra data structures or complex logic, as the problem is localized to checking each window of three numbers as we scan the array. This is much simpler than, for example, finding any three odd numbers anywhere in the array.
Let's break down the steps to solve this problem:
i
, check if arr[i]
, arr[i+1]
, and arr[i+2]
are all odd numbers. An integer is odd if num % 2 != 0
.
True
immediately.
False
.
This approach is optimal for the problem, as it only requires a single pass through the array and constant extra space.
Let's consider the sample input arr = [2, 6, 4, 1, 3, 5, 9]
.
[2, 6, 4]
→ 2 is even, skip.[6, 4, 1]
→ 6 is even, skip.[4, 1, 3]
→ 4 is even, skip.[1, 3, 5]
→ 1 is odd, 3 is odd, 5 is odd. All are odd!
Since we've found three consecutive odd numbers at indices 3, 4, and 5, we return True
immediately.
class Solution:
def threeConsecutiveOdds(self, arr):
for i in range(len(arr) - 2):
if arr[i] % 2 == 1 and arr[i+1] % 2 == 1 and arr[i+2] % 2 == 1:
return True
return False
class Solution {
public:
bool threeConsecutiveOdds(vector<int>& arr) {
for (int i = 0; i < arr.size() - 2; ++i) {
if (arr[i] % 2 == 1 && arr[i+1] % 2 == 1 && arr[i+2] % 2 == 1)
return true;
}
return false;
}
};
class Solution {
public boolean threeConsecutiveOdds(int[] arr) {
for (int i = 0; i <= arr.length - 3; i++) {
if (arr[i] % 2 == 1 && arr[i+1] % 2 == 1 && arr[i+2] % 2 == 1) {
return true;
}
}
return false;
}
}
var threeConsecutiveOdds = function(arr) {
for (let i = 0; i <= arr.length - 3; i++) {
if (arr[i] % 2 === 1 && arr[i+1] % 2 === 1 && arr[i+2] % 2 === 1) {
return true;
}
}
return false;
};
O(n)
, where n is the length of arr
.
O(1)
, as we only use a constant amount of extra space (no additional data structures).
The "Three Consecutive Odds" problem is a great example of a localized sliding window check. By simply iterating through the array and examining each group of three consecutive numbers, we can efficiently determine if any such group contains all odd numbers. The solution is elegant in its simplicity, requiring only a single pass and no extra space, making it both easy to understand and optimal for performance.