# Length of the longest alternating even odd subarray

Given an **array a[] of N** integers, the task is to find the length of the longest Alternating Even Odd subarray present in the array.

**Examples:**

Input:a[] = {1, 2, 3, 4, 5, 7, 9}

Output:5

Explanation:

The subarray {1, 2, 3, 4, 5} has alternating even and odd elements.

Input:a[] = {1, 3, 5}

Output:0

Explanation:

There is no such alternating sequence possible.

**Approach:** To solve the problem mentioned above we have to observe that the **Sum of two even** numbers is even, **Sum of two odd** numbers is even but **sum of one even and one odd** number is odd.

- Initially initialize
*cnt*a counter to store the length as 1. - Iterate among the array elements, check if consecutive elemnts has an odd sum.
- Increase the cnt by 1 if it has a odd sum.
- If it does not has a odd sum, then re-initialize cnt by 1.

Below is the implementation of the above approach:

## C++

`// C++ program to find the Length of the ` `// longest alternating even odd subarray ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the longest subarray ` `int` `longestEvenOddSubarray(` `int` `a[], ` `int` `n) ` `{ ` ` ` `// Length of longest ` ` ` `// alternating subarray ` ` ` `int` `longest = 1; ` ` ` `int` `cnt = 1; ` ` ` ` ` `// Iterate in the array ` ` ` `for` `(` `int` `i = 0; i < n - 1; i++) { ` ` ` ` ` `// increment count if consecutive ` ` ` `// elements has an odd sum ` ` ` `if` `((a[i] + a[i + 1]) % 2 == 1) { ` ` ` `cnt++; ` ` ` `} ` ` ` `else` `{ ` ` ` `// Store maximum count in longest ` ` ` `longest = max(longest, cnt); ` ` ` ` ` `// Reinitialize cnt as 1 consecutive ` ` ` `// elements does not have an odd sum ` ` ` `cnt = 1; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Length of 'longest' can never be 1 ` ` ` `// since even odd has to occur in pair or more ` ` ` `// so return 0 if longest = 1 ` ` ` `if` `(longest == 1) ` ` ` `return` `0; ` ` ` ` ` `return` `max(cnt, longest); ` `} ` ` ` `/* Driver code*/` `int` `main() ` `{ ` ` ` `int` `a[] = { 1, 2, 3, 4, 5, 7, 8 }; ` ` ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` ` ` `cout << longestEvenOddSubarray(a, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the Length of the ` `// longest alternating even odd subarray ` `import` `java.util.*; ` `class` `GFG{ ` ` ` `// Function to find the longest subarray ` `static` `int` `longestEvenOddSubarray(` `int` `a[], ` `int` `n) ` `{ ` ` ` `// Length of longest ` ` ` `// alternating subarray ` ` ` `int` `longest = ` `1` `; ` ` ` `int` `cnt = ` `1` `; ` ` ` ` ` `// Iterate in the array ` ` ` `for` `(` `int` `i = ` `0` `; i < n - ` `1` `; i++) ` ` ` `{ ` ` ` ` ` `// increment count if consecutive ` ` ` `// elements has an odd sum ` ` ` `if` `((a[i] + a[i + ` `1` `]) % ` `2` `== ` `1` `) ` ` ` `{ ` ` ` `cnt++; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `// Store maximum count in longest ` ` ` `longest = Math.max(longest, cnt); ` ` ` ` ` `// Reinitialize cnt as 1 consecutive ` ` ` `// elements does not have an odd sum ` ` ` `cnt = ` `1` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Length of 'longest' can never be 1 ` ` ` `// since even odd has to occur in pair or more ` ` ` `// so return 0 if longest = 1 ` ` ` `if` `(longest == ` `1` `) ` ` ` `return` `0` `; ` ` ` ` ` `return` `Math.max(cnt, longest); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `a[] = { ` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `, ` `7` `, ` `8` `}; ` ` ` ` ` `int` `n = a.length; ` ` ` ` ` `System.out.println(longestEvenOddSubarray(a, n)); ` `} ` `} ` ` ` `// This code is contributed by offbeat ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the length of the ` `# longest alternating even odd subarray ` ` ` `# Function to find the longest subarray ` `def` `longestEvenOddSubarray(arr, n): ` ` ` ` ` `# Length of longest ` ` ` `# alternating subarray ` ` ` `longest ` `=` `1` ` ` `cnt ` `=` `1` ` ` ` ` `# Iterate in the array ` ` ` `for` `i ` `in` `range` `(n ` `-` `1` `): ` ` ` ` ` `# Increment count if consecutive ` ` ` `# elements has an odd sum ` ` ` `if` `((arr[i] ` `+` `arr[i ` `+` `1` `]) ` `%` `2` `=` `=` `1` `): ` ` ` `cnt ` `=` `cnt ` `+` `1` ` ` `else` `: ` ` ` ` ` `# Store maximum count in longest ` ` ` `longest ` `=` `max` `(longest, cnt) ` ` ` ` ` `# Reinitialize cnt as 1 consecutive ` ` ` `# elements does not have an odd sum ` ` ` `cnt ` `=` `1` ` ` ` ` `# Length of 'longest' can never be 1 since ` ` ` `# even odd has to occur in pair or more ` ` ` `# so return 0 if longest = 1 ` ` ` `if` `(longest ` `=` `=` `1` `): ` ` ` `return` `0` ` ` ` ` `return` `max` `(cnt, longest) ` ` ` `# Driver Code ` `arr ` `=` `[ ` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `, ` `7` `, ` `8` `] ` `n ` `=` `len` `(arr) ` ` ` `print` `(longestEvenOddSubarray(arr, n)) ` ` ` `# This code is contributed by skylags ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the Length of the ` `// longest alternating even odd subarray ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to find the longest subarray ` `static` `int` `longestEvenOddSubarray(` `int` `[] a, ` `int` `n) ` `{ ` ` ` ` ` `// Length of longest ` ` ` `// alternating subarray ` ` ` `int` `longest = 1; ` ` ` `int` `cnt = 1; ` ` ` ` ` `// Iterate in the array ` ` ` `for` `(` `int` `i = 0; i < n - 1; i++) ` ` ` `{ ` ` ` ` ` `// Increment count if consecutive ` ` ` `// elements has an odd sum ` ` ` `if` `((a[i] + a[i + 1]) % 2 == 1) ` ` ` `{ ` ` ` `cnt++; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` ` ` `// Store maximum count in longest ` ` ` `longest = Math.Max(longest, cnt); ` ` ` ` ` `// Reinitialize cnt as 1 consecutive ` ` ` `// elements does not have an odd sum ` ` ` `cnt = 1; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Length of 'longest' can never be 1 ` ` ` `// since even odd has to occur in pair ` ` ` `// or more so return 0 if longest = 1 ` ` ` `if` `(longest == 1) ` ` ` `return` `0; ` ` ` `return` `Math.Max(cnt, longest); ` `} ` ` ` `// Driver code ` `static` `void` `Main() ` `{ ` ` ` ` ` `int` `[] a = { 1, 2, 3, 4, 5, 7, 8 }; ` ` ` `int` `n = a.Length; ` ` ` ` ` `Console.WriteLine(longestEvenOddSubarray(a, n)); ` `} ` `} ` ` ` `// This code is contributed by divyeshrabadiya07 ` |

*chevron_right*

*filter_none*

**Output:**

5

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Length of the longest alternating subarray
- Longest alternating (positive and negative) subarray starting at every index
- Length of longest subarray of length at least 2 with maximum GCD
- Length of the longest Subarray with only Even Elements
- Length of longest subarray with negative product
- Length of the longest ZigZag subarray of the given array
- Length of longest increasing circular subarray
- Length of longest Subarray with equal number of odd and even elements
- Length of longest subarray with product greater than or equal to 0
- Length of Longest Subarray with same elements in atmost K increments
- Length of longest Fibonacci subarray formed by removing only one element
- Find the length of the longest subarray with atmost K occurrences of the integer X
- Longest alternating subsequence which has maximum sum of elements
- Longest alternating subsequence in terms of positive and negative integers
- Maximum length of subarray such that all elements are equal in the subarray
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Maximum length of subarray such that sum of the subarray is even
- Longest alternating sub-array starting from every index in a Binary Array
- Longest subarray having sum K | Set 2

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.