Length of the longest alternating even odd subarray

Last Updated : 30 Jun, 2026

Given an array arr[], return the maximum possible length of a subarray such that its elements are arranged alternately either as even and odd or odd and even.

Examples: 

Input: arr[] = [10, 12, 14, 7, 8]
Output: 3 
Explanation: The max length of subarray is 3 and the subarray is [14, 7, 8]. Here the array starts as an even element and has odd and even elements alternately.

Input: arr[] = [4, 6]
Output: 1 
Explanation: The array contains [4, 6]. So, we can only choose 1 element as that will be the max length subarray.

Try It Yourself
redirect icon

[Naive Approach] Brute Force - O(n^2) Time and O(1) Space

The brute force approach involves checking every possible subarray to find the longest alternating sequence. We use an outer loop to pick a starting index and an inner loop to examine subsequent elements, keeping a running count as long as the even-odd pattern holds. Whenever the alternating sequence breaks, you compare its length against the maximum length found so far, update the maximum if necessary, and then move to the next starting index.

C++
#include <iostream>
#include <vector>
using namespace std;

int maxEvenOdd(vector<int>& arr)
{
    int n = arr.size();
    // Length of longest alternating subarray
    int ans = 1;

    // Iterate in the array
    for (int i = 0; i < n; i++) {
        int cnt = 1;
        
        // Iterate for every  subarray
        for (int j = i + 1; j < n; j++) {
            if ((arr[j - 1] % 2)!= (arr[j] % 2))
                cnt++;
            else
                break;
        }
        
        // store max count
        ans = max(ans, cnt);
    }  
    
    return ans;
}

int main()
{
    vector<int> arr = { 1, 2, 3, 4, 5, 7, 8 }; 

    cout << maxEvenOdd(arr);
    return 0;
}
Java
import java.util.Arrays;

public class GFG {
    public static int maxEvenOdd(int[] arr) {
        int n = arr.length;
        
        // Length of longest alternating subarray
        int ans = 1;

        // Iterate in the array
        for (int i = 0; i < n; i++) {
            int cnt = 1;

            // Iterate for every subarray
            for (int j = i + 1; j < n; j++) {
                if ((arr[j - 1] % 2)!= (arr[j] % 2))
                    cnt++;
                else
                    break;
            }

            // store max count
            ans = Math.max(ans, cnt);
        }

        return ans;
    }

    public static void main(String[] args) {
        int[] arr = { 1, 2, 3, 4, 5, 7, 8 };

        System.out.println(maxEvenOdd(arr));
    }
}
Python
def maxEvenOdd(arr):
    n = len(arr)
    
    # Length of longest alternating subarray
    ans = 1

    # Iterate in the array
    for i in range(n):
        cnt = 1

        # Iterate for every subarray
        for j in range(i + 1, n):
            if (arr[j - 1] % 2)!= (arr[j] % 2):
                cnt += 1
            else:
                break

        # store max count
        ans = max(ans, cnt)

    return ans

if __name__ == "__main__":
    arr = [1, 2, 3, 4, 5, 7, 8]
    
    print(maxEvenOdd(arr))
C#
using System;

public class GFG {
    public static int maxEvenOdd(int[] arr) {
        int n = arr.Length;
        
        // Length of longest alternating subarray
        int ans = 1;

        // Iterate in the array
        for (int i = 0; i < n; i++) {
            int cnt = 1;

            // Iterate for every subarray
            for (int j = i + 1; j < n; j++) {
                if ((arr[j - 1] % 2)!= (arr[j] % 2))
                    cnt++;
                else
                    break;
            }

            // store max count
            ans = Math.Max(ans, cnt);
        }

        return ans;
    }

    public static void Main() {
        int[] arr = { 1, 2, 3, 4, 5, 7, 8 };

        Console.WriteLine(maxEvenOdd(arr));
    }
}
JavaScript
function maxEvenOdd(arr) {
    let n = arr.length;
    
    // Length of longest alternating subarray
    let ans = 1;

    // Iterate in the array
    for (let i = 0; i < n; i++) {
        let cnt = 1;

        // Iterate for every subarray
        for (let j = i + 1; j < n; j++) {
            if ((arr[j - 1] % 2)!== (arr[j] % 2))
                cnt++;
            else
                break;
        }

        // store max count
        ans = Math.max(ans, cnt);
    }

    return ans;
}

// Driver code
let arr = [1, 2, 3, 4, 5, 7, 8];

console.log(maxEvenOdd(arr));

Output
5

[Expected Approach] Single Pass - O(n) Time and O(1) Space

The optimal approach walks through the array exactly once, keeping a running counter for the current sequence length and another for the maximum length found. Whenever the current number and the previous number have different parities (one even, one odd), you increase your running counter by one. If they have the same parity, the pattern breaks, so you immediately reset the running counter back to one and continue checking.

Let's understand with an example:
Consider an array : [10, 15, 12, 14, 17]

  • i = 0 : First number is 10; current length = 1, max length = 1.
  • i = 1 : Parities of arr[i] and arr[i-1] are different, current length = 2, max length = 2.
  • i = 2 : Parities of arr[i] and arr[i-1] are different, current length = 3, max length = 3.
  • i = 3 : Parities of arr[i] and arr[i-1] are same, current length resets to 1, max length stays 3.
  • i = 4 : Parities of arr[i] and arr[i-1] are different, current length = 2, max length stays 3.
C++
#include <bits/stdc++.h>
using namespace std;

int maxEvenOdd(vector<int>& arr)
{
    int n = arr.size();
    
    // Length of longest alternating subarray
    int cur = 1;
    
    int maxLen = 1;
    
    for (int i = 1; i < n ; i++) {
        
        // If different parities increase current length by 1  
        if ((arr[i] % 2) != (arr[i - 1] % 2)) 
            cur++;
            
        // Reset current length to 1 
        else
            cur = 1; 
            
        maxLen = max(maxLen, cur);
    } 
    return maxLen;
}

int main()
{
    vector<int> arr = { 1, 2, 3, 4, 5, 7, 8 }; 

    cout << maxEvenOdd(arr);
    return 0;
}
Java
import java.util.*;

public class GFG {
    static int maxEvenOdd(int[] arr) {
        int n = arr.length;
        
        // Length of longest alternating subarray
        int cur = 1;
        
        int maxLen = 1;
        
        for (int i = 1; i < n; i++) {
            
            // If different parities increase current length by 1  
            if ((arr[i] % 2)!= (arr[i - 1] % 2)) 
                cur++;
            
            // Reset current length to 1 
            else
                cur = 1;
            
            maxLen = Math.max(maxLen, cur);
        }
        return maxLen;
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 7, 8}; 
        System.out.println(maxEvenOdd(arr));
    }
}
Python
def maxEvenOdd(arr):
    n = len(arr)
    
    # Length of longest alternating subarray
    cur = 1
    
    maxLen = 1
    
    for i in range(1, n):
        
        # If different parities increase current length by 1  
        if (arr[i] % 2)!= (arr[i - 1] % 2): 
            cur += 1
            
        # Reset current length to 1 
        else:
            cur = 1
         
        maxLen = max(maxLen, cur)
     
    return maxLen

if __name__ == "__main__":
    arr = [1, 2, 3, 4, 5, 7, 8]
    print(maxEvenOdd(arr))
C#
using System;

public class GFG {
    static int maxEvenOdd(int[] arr) {
        int n = arr.Length;
        
        // Length of longest alternating subarray
        int cur = 1;
        
        int maxLen = 1;
        
        for (int i = 1; i < n; i++) {
            
            // If different parities increase current length by 1  
            if ((arr[i] % 2)!= (arr[i - 1] % 2)) 
                cur++;
            
            // Reset current length to 1 
            else
                cur = 1;
            
            maxLen = Math.Max(maxLen, cur);
        }
        return maxLen;
    }

    public static void Main() {
        int[] arr = {1, 2, 3, 4, 5, 7, 8}; 
        Console.WriteLine(maxEvenOdd(arr));
    }
}
JavaScript
function maxEvenOdd(arr) {
    const n = arr.length;
    
    // Length of longest alternating subarray
    let cur = 1;
    
    let maxLen = 1;
    
    for (let i = 1; i < n; i++) {
        
        // If different parities increase current length by 1  
        if ((arr[i] % 2)!== (arr[i - 1] % 2)) 
            cur++;
        
        // Reset current length to 1 
        else
            cur = 1;
        
        maxLen = Math.max(maxLen, cur);
    }
    return maxLen;
}

// Driver code
const arr = [1, 2, 3, 4, 5, 7, 8];
console.log(maxEvenOdd(arr));

Output
5


Comment