# 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.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++ program to find the Length of the ` `// longest alternating even odd subarray ` ` `  `#include ` `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; ` `} `

 `// 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 `

 `# 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 `

 `// 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 `

Output:
```5
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

Article Tags :
Practice Tags :