LeetCode 1550: Three Consecutive Odds Solution
Master LeetCode problem 1550 (Three Consecutive Odds), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1550. Three Consecutive Odds
Problem Explanation
Explanation:
To solve this problem, we can iterate through the array and check if there are three consecutive odd numbers. We can do this by maintaining a count of consecutive odd numbers encountered so far. If the count reaches 3, we return true. Otherwise, we reset the count if we encounter an even number.
- Start by initializing a count variable to 0.
- Iterate through the array elements.
- If the current element is odd, increment the count.
- If the current element is even, reset the count to 0.
- If at any point the count reaches 3, return true.
- If we reach the end of the array without finding three consecutive odds, return false.
Time Complexity:
The time complexity of this approach is O(n), where n is the number of elements in the input array.
Space Complexity:
The space complexity is O(1) as we are using a constant amount of extra space regardless of the input size.
Solution Code
class Solution {
public boolean threeConsecutiveOdds(int[] arr) {
int count = 0;
for (int num : arr) {
if (num % 2 != 0) {
count++;
if (count == 3) {
return true;
}
} else {
count = 0;
}
}
return false;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1550 (Three Consecutive Odds)?
This page provides optimized solutions for LeetCode problem 1550 (Three Consecutive Odds) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1550 (Three Consecutive Odds)?
The time complexity for LeetCode 1550 (Three Consecutive Odds) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1550 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1550 in Java, C++, or Python.