Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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] <= 1000Problem summary: Given an integer array arr, return true if there are three consecutive odd numbers in the array. Otherwise, return false.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[2,6,4,1]
[1,2,34,3,4,5,7,23,12]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1550: Three Consecutive Odds
class Solution {
public boolean threeConsecutiveOdds(int[] arr) {
int cnt = 0;
for (int x : arr) {
if (x % 2 == 1) {
if (++cnt == 3) {
return true;
}
} else {
cnt = 0;
}
}
return false;
}
}
// Accepted solution for LeetCode #1550: Three Consecutive Odds
func threeConsecutiveOdds(arr []int) bool {
cnt := 0
for _, x := range arr {
if x&1 == 1 {
cnt++
if cnt == 3 {
return true
}
} else {
cnt = 0
}
}
return false
}
# Accepted solution for LeetCode #1550: Three Consecutive Odds
class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
cnt = 0
for x in arr:
if x & 1:
cnt += 1
if cnt == 3:
return True
else:
cnt = 0
return False
// Accepted solution for LeetCode #1550: Three Consecutive Odds
struct Solution;
impl Solution {
fn three_consecutive_odds(arr: Vec<i32>) -> bool {
arr.windows(3)
.any(|w| w[0] % 2 == 1 && w[1] % 2 == 1 && w[2] % 2 == 1)
}
}
#[test]
fn test() {
let arr = vec![2, 6, 4, 1];
let res = false;
assert_eq!(Solution::three_consecutive_odds(arr), res);
let arr = vec![1, 2, 34, 3, 4, 5, 7, 23, 12];
let res = true;
assert_eq!(Solution::three_consecutive_odds(arr), res);
}
// Accepted solution for LeetCode #1550: Three Consecutive Odds
function threeConsecutiveOdds(arr: number[]): boolean {
let cnt = 0;
for (const x of arr) {
if (x & 1) {
if (++cnt == 3) {
return true;
}
} else {
cnt = 0;
}
}
return false;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.