VOOZH about

URL: https://www.geeksforgeeks.org/dsa/length-of-the-longest-alternating-even-odd-subarray/

⇱ Length of the longest alternating even odd subarray - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Length of the longest alternating even odd subarray

Last Updated : 12 Jul, 2025

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:
Explanation:
The subarray {1, 2, 3, 4, 5} has alternating even and odd elements.

Input: a[] = {1, 3, 5} 
Output:
Explanation:
There is no such alternating sequence possible. 

The idea is to consider every subarray and find the length of even and odd subarrays.

Follow the steps below to solve the problem:

  • Iterate for every subarray from i = 0 
  • Make a nested loop, iterate from j = i + 1
  • Now, check if a[j - 1] is even and a[j] is odd or a[j - 1] is odd and a[j] is even then increment count
  • Maintain an answer variable which calculates max count so far

Below is the implementation of the above approach:


Output
5

Time Complexity: O(N2), Iterating over every subarray therefore N2 are possible
Auxiliary Space: O(1)

Observe that the Sum of two even numbers is even, the Sum of two odd numbers is even but the sum of one even and one odd number is odd.

Follow the steps below to solve the problem:

  • Initially initialize cnt a counter to store the length as 1.
  • Iterate among the array elements, and check if consecutive elements have an odd sum.
  • Increase the cnt by 1 if it has an odd sum.
  • If it does not has an odd sum, then re-initialize cnt by 1. 
  • The function should return at least value 1 if there are elements in the array, because there can always be a subarray with length 1 which has either odd or even element.

Below is the implementation of the above approach:


Output
5

Time Complexity: O(N), Traversing over the array one time.
Auxiliary Space: O(1)

By simply storing the nature of the previous element we encounter( odd or even) and comparing it with the next element.

Follow the steps below to solve the problem:

  • Initialize a variable maxLength to 0, to keep the track of maximum length of the alternating subarray obtained.
  • Initialize a variable currLen to 1 considering first element as the part of alternating subarray.
  • Starting with element at index 1, compare every element with it's previous. If there nature are different, increment the currLen variable.
  • Otherwise, reset the currLen to 1 again so that, this current element is considered in new alternating subarray.
  • Keep storing the max length of subarray in maxLength before resetting the currLen.
  • Return the found max length of subarray.

Below is the implementation of above approach:


Output
Length of longest subarray of even and odds is : 5

Time Complexity: O(N), Since we need to iterate over the whole array once
Auxiliary Space: O(1)

Comment
Article Tags: