## Problem Statement

In ” Three Consecutive Odds ” problem we have been given an array and we have to check if there are three consecutive odd numbers in the array present or not. If it is present then we have to return true or else we will return false.

### Example

arr = [2,6,4,1]

false

Explanation :

There are no three consecutive odds. Hence return false.

arr = [1,2,34,3,4,5,7,23,12]

true

Explanation:

In given array if we check for all three consecutive element segments. It would be as follows :

[ 5 , 7 , 23 ] are three consecutive odds. Hence return true.

## Approach

It is a simple problem which can be solved using a single traversal of the given array. During the traversal we have to check for all three consecutive elements. The point we find all the three numbers to be odd, we return true.

For this we can use a for loop and we can iterate for the third element of each group ( 3 consecutive elements ) from index=2 to index=n-1. Then the current consecutive segment will be represented by elements arr[i-2] , arr[i-1] and arr[i].

We will start iterating from third element from front. If size of the array is less than three then we return false.

### Algorithm

- Create a variable i and initialise with the index 2.
- Run a for loop for i till the last element, (n-1)th index.
- Check if element at indices i, (i-1) and (i-2) is odd or not.
- If all three are odd, return true. Else continue the traversal.
- After traversing all indices, return false.

## Implementation

### C++ Program for Three Consecutive Odds Leetcode Solution

#include <bits/stdc++.h> using namespace std; bool threeConsecutiveOdds(vector<int>& arr) { int n=arr.size(); for(int i = 2; i < n; i++) { if(arr[i] % 2 == 1 && arr[i-1] % 2 == 1 && arr[i-2] % 2 == 1 ) return true; } return false; } int main() { vector<int> arr={1,2,34,3,4,5,7,23,12}; if(threeConsecutiveOdds(arr) ) cout<<"true"<<endl; else cout<<"no"<<endl; return 0; }

true

### Java Program for Three Consecutive Odds Leetcode Solution

import java.lang.*; class Rextester { public static boolean threeConsecutiveOdds(int[] arr) { int n=arr.length; for(int i = 2; i < n; i++) { if(arr[i] % 2 == 1 && arr[i-1] % 2 == 1 && arr[i-2] % 2 == 1 ) return true; } return false; } public static void main(String args[]) { int[] arr={1,2,34,3,4,5,7,23,12}; System.out.println(threeConsecutiveOdds(arr)); } }

true

## Complexity Analysis for Three Consecutive Odds Leetcode Solution

**Time Complexity**

**O(N) :** Where N is the size of the given array. As we are traversing only once for each index, the time complexity will be O(N).

**Space Complexity **

**O(1) :** We are not using any extra memory. Hence space complexity will be constant.