Length of the longest Subarray with only Even Elements

Given an array arr[]. The task is to find the length of the longest subarray of arr[] such that it contains only even elements.

Examples:

Input : arr[] = { 5, 2, 4, 7 }
Output : Length = 2
subArr[] = {2, 4}

Input : arr[] = {9, 8, 5, 4, 4, 4, 2, 4, 1}
Output : Length 5
subArr[] = {4, 4, 4, 2, 4}

The idea is to observe that the largest subarray with only even elements is the maximum number of contiguous even elements in the array. Therefore, the task now reduces to find the maximum number of contiguous even elements in the array.

To do this, traverse the array using two variables, ans and current_count. The variable ans stores the final answer and current_count stores the length of subarray with only even numbers.

Now whenever an even element is found, keep incrementing the current_count and whenever an ODD element is found take the maximum of ans and current_count and reset current_count to zero.

At the end, ans will store the length of largest subarray with only even elements.

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
// largest Subarray with only even elements
  
#include <cmath>
#include <iostream>
using namespace std;
  
// Function to find the Length of the
// largest Subarray with only even elements
int maxEvenSubarray(int array[], int N)
{
    int ans = 0;
    int count = 0;
  
    // Iterate the loop
    for (int i = 0; i < N; i++) {
        // Check whether the element is
        // even in continuous fashion
        if (array[i] % 2 == 0) {
            count++;
        }
        else {
            // If element are not even in continuous
            // fashion, Reinitialize the count
            ans = max(ans, count);
            count = 0;
        }
    }
  
    // Check for the last element in the array
    ans = max(ans, count);
    return ans;
}
  
// Driver Code
int main()
{
    int arr[] = { 9, 8, 5, 4, 4, 4, 2, 4, 1 };
  
    int N = sizeof(arr) / sizeof(arr[0]);
  
    cout << maxEvenSubarray(arr, 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
// Subarray with only Even Elements
public class GFG {
  
    // Function to find the Length of the longest
    // Subarray with only Even Elements
    static int maxEvenSubarray(int array[], int N)
    {
        int ans = 0;
        int count = 0;
  
        // Iterate the loop
        for (int i = 0; i < array.length; i++) {
            // Check whether the element is
            // even in continuous fashion
            if (array[i] % 2 == 0) {
                count++;
            }
            else {
                // If element are not even in continuous
                // fashion. Reinitialize the count
                ans = Math.max(ans, count);
                count = 0;
            }
        }
  
        // Check for the last element in the array
        ans = Math.max(ans, count);
        return ans;
    }
  
    // Driver Code
    public static void main(String args[])
    {
        int arr[] = { 9, 8, 5, 4, 4, 4, 2, 4, 1 };
  
        int N = arr.length;
  
        System.out.println(maxEvenSubarray(arr, N));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 Program to find the Length of the 
# largest Subarray with only even elements 
  
# Function to find the Length of the
# largest Subarray with only even elements
def maxEvenSubarray(array,N):
    ans = 0
    count = 0
  
    # Iterate the loop
    for i in range(0,N):
          
        # Check whether the element is
        # even in continuous fashion
        if array[i]%2==0:
            count +=1
        else:
            # If element are not even in continuous
            # fashion, Reinitialize the count
            ans = max(ans,count)
            count = 0
              
    # Check for the last element in the array
    ans = max(ans,count)
    return ans
  
# Driver code
if __name__=='__main__':
    arr = [9, 8, 5, 4, 4, 4, 2, 4, 1]
    N = len(arr)
    print(maxEvenSubarray(arr,N))
  
# This article is contributed by Shrikant13

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to find the Length 
// of the largest Subarray with 
// only even elements
using System;
  
class GFG
{
// Function to find the Length 
// of the largest Subarray with
// only even elements
static int maxEvenSubarray(int []array, 
                           int N)
{
    int ans = 0;
    int count = 0;
  
    // Iterate the loop
    for (int i = 0; i < N; i++) 
    {
        // Check whether the element is
        // even in continuous fashion
        if (array[i] % 2 == 0)
        {
            count++;
        }
        else
        {
            // If element are not even in
            // continuous fashion,
            // Reinitialize the count
            ans = Math.Max(ans, count);
            count = 0;
        }
    }
  
    // Check for the last 
    // element in the array
    ans = Math.Max(ans, count);
    return ans;
}
  
// Driver Code
public static void Main()
{
    int []arr = { 9, 8, 5, 4, 
                  4, 4, 2, 4, 1 };
  
    int N = arr.Length;
  
    Console.WriteLine(maxEvenSubarray(arr, N));
}
}
  
// This code is contributed by ihritik

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to find the Length 
// of the largest Subarray with 
// only even elements
  
// Function to find the Length 
// of the largest Subarray with
// only even elements
function maxEvenSubarray($array, $N)
{
    $ans = 0;
    $count = 0;
  
    // Iterate the loop
    for ($i = 0; $i < $N; $i++) 
    {
        // Check whether the element is
        // even in continuous fashion
        if ($array[$i] % 2 == 0) 
        {
            $count++;
        }
        else 
        {
            // If element are not even in 
            // continuous fashion, 
            // Reinitialize the count
            $ans = max($ans, $count);
            $count = 0;
        }
    }
  
    // Check for the last
    // element in the array
    $ans = max($ans, $count);
    return $ans;
}
  
// Driver Code
$arr = array( 9, 8, 5, 4, 
              4, 4, 2, 4, 1 );
$N = sizeof($arr);
  
echo maxEvenSubarray($arr, $N);
  
// This code is contributed by ihritik
?>

chevron_right


Output:

5

Time Complexity: O(n)



My Personal Notes arrow_drop_up

सर्वशक्तिशाली इकलौता

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.



Improved By : shrikanth13, ihritik