
Problem Statement
In the given problem, you are required to determine whether an integer array arr
contains three consecutive elements where all three elements are odd numbers. The function should return a boolean value true
if such a triplet exists within the array, and false
otherwise. The array's elements are strictly positive and the length of the array can vary from 1 to 1000.
Examples
Example 1
Input:
arr = [2,6,4,1]
Output:
false
Explanation:
There are no three consecutive odds.
Example 2
Input:
arr = [1,2,34,3,4,5,7,23,12]
Output:
true
Explanation:
[5,7,23] are three consecutive odds.
Constraints
1 <= arr.length <= 1000
1 <= arr[i] <= 1000
Approach and Intuition
To solve this problem, we must check for the presence of three consecutive odd numbers in the array. Here is a straightforward method to achieve this:
- Traverse through the array from the first element to the third last element because we need a group of three for our condition.
- For each element
arr[i]
, check ifarr[i]
,arr[i+1]
, andarr[i+2]
are all odd numbers. - An odd number can be identified by checking
number % 2 != 0
. - If we find such a triplet, immediately return
true
. - If the loop completes and no trio of consecutive odd numbers is found, return
false
.
This approach allows us to efficiently determine the presence of three consecutive odd numbers by checking each potentially qualifying sequence only once. The constraints ensure that this method will execute in a reasonable time frame for all inputs likely to be encountered.
Solutions
- C++
- Java
- Python
class Solution {
public:
bool hasThreeConsecutiveOdds(vector<int>& data) {
int count = data.size();
for (int idx = 0; idx < count - 2; idx++) {
int result = data[idx] * data[idx + 1] * data[idx + 2];
if (result % 2 != 0) return true;
}
return false;
}
};
The provided C++ program defines a function hasThreeConsecutiveOdds
within the Solution
class that checks for three consecutive odd numbers in an input vector of integers. Understand the approach below:
- The function accepts a vector of integers named
data
. - It calculates the length of the vector and stores it in the variable
count
. - A loop iterates through the vector intensively up to the third-last element, ensuring there are always three elements to check consecutively.
- During each iteration, the function multiplies three consecutive values starting from the current index position.
- It then checks if the product of these three numbers is odd, which is done by verifying if the product modulo 2 is not zero.
- If an odd product is found, it implies the three numbers are all odds, hence it returns
true
. - If the loop completes without finding such a triplet, the function returns
false
.
Ensure that the input vector has at least three elements; otherwise, the loop does not execute, and the function will return false
by default. This method leverages multiplication to check for odd numbers efficiently without explicitly checking each number's oddness.
class Solution {
public boolean hasThreeConsecutiveOdds(int[] nums) {
for (int index = 0; index < nums.length - 2; index++) {
int calcProduct = nums[index] * nums[index + 1] * nums[index + 2];
if (calcProduct % 2 != 0) return true;
}
return false;
}
}
The Java solution provided aims to determine if an input array nums
contains three consecutive odd numbers. The method hasThreeConsecutiveOdds
iterates through the array with a for loop, checking each group of three consecutive elements.
- Initialize the loop with
index
starting from 0 and going up tonums.length - 2
to prevent out-of-bounds access when referencingnums[index + 2]
. - Calculate the product of the three numbers at the positions
index
,index + 1
, andindex + 2
. - Check if the calculated product is odd, which implies all three numbers are odd because the product of three integers is odd only if all of them are odd.
- Return
true
immediately if a triplet of consecutive odd elements is found. - If the loop completes without finding such a triplet, return
false
.
This approach leverages the mathematical property that only odd numbers have an odd product, providing an efficient way to verify the odd triplet condition without explicitly checking the oddness of each individual element.
class Solution:
def hasThreeConsecutiveOdds(self, numbers: list[int]) -> bool:
for index in range(len(numbers) - 2):
multiplied_values = numbers[index] * numbers[index + 1] * numbers[index + 2]
if multiplied_values % 2 != 0:
return True
return False
The provided Python code defines a method hasThreeConsecutiveOdds
, aiming to check if an input list contains three consecutive odd numbers. It initializes by iterating through the list with the help of a for
loop, limiting the loop's range to accommodate groups of three consecutive elements.
Inside the loop, the code calculates the product of three consecutive elements. By checking if this product is odd (using % 2 != 0
), it exploits the property that the product of odd numbers is always odd. If such a group is found, the method immediately returns True
, indicating the presence of three consecutive odd numbers. If the loop completes without finding such a sequence, the method returns False
. This solution efficiently checks for the required condition without unnecessary computations.
No comments yet.