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++

filter_none

edit
close

play_arrow

link
brightness_4
code

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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.