Advertisement
1550. Three Consecutive Odds
UnknownView on LeetCode
Time: O(n)
Space: O(1)
Approach
Linear scan counting consecutive odd numbers; reset on even.
1550.cs
C#
// Approach: Linear scan counting consecutive odd numbers; reset on even.
// Time: O(n) Space: O(1)
public class Solution
{
public bool ThreeConsecutiveOdds(int[] arr)
{
int cnt = 0;
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] % 2 > 0)
cnt++;
else
cnt = 0;
if (cnt == 3)
return true;
}
return false;
}
}Advertisement
Was this solution helpful?