arr, return true if there are three consecutive odd numbers in the array. Otherwise, return false.
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 <= 10001 <= arr[i] <= 1000When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute-force method for this problem is to examine every possible set of three consecutive numbers. We check each group to see if they are all odd. If we find such a group, we are done.
Here's how the algorithm would work step-by-step:
def three_consecutive_odds(numbers):
list_length = len(numbers)
# Iterate through the list, checking sets of three numbers.
for index in range(list_length - 2):
# Check if the current three numbers are all odd.
if (numbers[index] % 2 != 0 and
numbers[index + 1] % 2 != 0 and
numbers[index + 2] % 2 != 0):
# If all three are odd, return True.
return True
# If the loop finishes without finding three consecutive odds, return False.
return FalseThe problem asks us to check if a list of numbers contains three odd numbers in a row. The most efficient approach is to simply go through the list once, checking each group of three consecutive numbers to see if they are all odd. As soon as we find such a group, we can stop and declare success.
Here's how the algorithm would work step-by-step:
def three_consecutive_odds(numbers):
for i in range(len(numbers) - 2):
# Iterate up to the point where 3 consecutive numbers can be checked.
if (numbers[i] % 2 != 0 and \
numbers[i+1] % 2 != 0 and \
numbers[i+2] % 2 != 0):
# Check if the current group of three are all odd.
return True # Return immediately if a match is found.
return False # No three consecutive odd numbers found.| Case | How to Handle |
|---|---|
| Null input array | Throw an IllegalArgumentException or return false, depending on language conventions. |
| Input array with fewer than 3 elements | Return false, as three consecutive odd numbers are impossible. |
| Input array with all even numbers | Return false as no three consecutive odd numbers are present. |
| Input array with very large numbers that could cause overflow during calculations (if applicable) | Use appropriate data types (e.g., long) to prevent integer overflow, or consider using a boolean check for oddness rather than mathematical operations. |
| Input array with negative odd numbers | The solution should correctly identify negative odd numbers; the parity check should handle negative odd numbers correctly. |
| Input array containing zero | Zero is even and should be handled correctly in the odd number check. |
| Array with alternating odd and even numbers | The solution will correctly identify that there are no three consecutive odd numbers. |
| Very large input array to test performance. | The solution should iterate through the array only once, providing O(n) time complexity. |