Find the count of maximum contiguous Even numbers

Given an array arr[] of N elements. The task is to find the maximum number of the contiguous even numbers in the given array.

Examples:

Input: arr[] = {1, 2, 3, 4, 6, 7}
Output: 2
Maximum contiguous even number sequence is {4, 6}

Input: arr[] = {1, 0, 2, 4, 3, 8, 9}
Output: 3
Maximum contiguous even number sequence is {0, 2, 4}

Approach: The idea is to keep traversing the array with two count variables named current_max and max_so_far. If an even number is found then increment current_max and compare it with max_so_far. Every time an odd element is found reset current_max to 0.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to count maximum
// contiguous even numbers
  
#include <iostream>
using namespace std;
  
// Function to count maximum
// contiguous even numbers
int countMaxContiguous(int arr[], int n)
{
    int current_max = 0, max_so_far = 0;
  
    for (int i = 0; i < n; i++) {
  
        // If current element is not even
        // reset current_max
        if (arr[i] % 2 != 0)
            current_max = 0;
  
        // If even element is found, increment
        // current_max and update result if
        // count becomes more
        else {
            current_max++; // increase count
            max_so_far = max(current_max, max_so_far);
        }
    }
  
    return max_so_far;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 0, 2, 4, 3, 8, 9 };
  
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << countMaxContiguous(arr, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to count maximum
// contiguous even numbers
import java.io.*;
  
class GFG
{
  
// Function to count maximum
// contiguous even numbers
static int countMaxContiguous(int arr[], int n)
{
    int current_max = 0, max_so_far = 0;
  
    for (int i = 0; i < n; i++)
    {
  
        // If current element is not 
        // even reset current_max
        if (arr[i] % 2 != 0)
            current_max = 0;
  
        // If even element is found, increment
        // current_max and update result if
        // count becomes more
        else
        {
            current_max++; // increase count
            max_so_far = Math.max(current_max, 
                                  max_so_far);
        }
    }
  
    return max_so_far;
}
  
// Driver code
public static void main (String[] args) 
{
    int arr[] = { 1, 0, 2, 4, 3, 8, 9 };
      
    int n = arr.length;
      
    System.out.println(countMaxContiguous(arr, n));
}
}
  
// This code is contributed
// by inder_verma

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to count maximum
# contiguous even numbers
  
# Function to count maximum
# contiguous even numbers
def countMaxContiguous(arr, n):
  
    current_max = 0
    max_so_far = 0
  
    for i in range(n) :
  
        # If current element is not
        # even reset current_max
        if (arr[i] % 2 != 0):
            current_max = 0
  
        # If even element is found,
        # increment current_max and 
        # update result if count 
        # becomes more
        else :
            current_max += 1 # increase count
            max_so_far = max(current_max, 
                             max_so_far)
  
    return max_so_far
  
# Driver code
if __name__ == "__main__":
      
    arr = [ 1, 0, 2, 4, 3, 8, 9 ]
  
    n = len(arr)
  
    print(countMaxContiguous(arr, n))
  
# This code is contributed
# by ChitraNayal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to count maximum
// contiguous even numbers
using System;
  
class GFG
{
  
// Function to count maximum
// contiguous even numbers
static int countMaxContiguous(int []arr, int n)
{
    int current_max = 0, max_so_far = 0;
  
    for (int i = 0; i < n; i++)
    {
  
        // If current element is not 
        // even reset current_max
        if (arr[i] % 2 != 0)
            current_max = 0;
  
        // If even element is found, increment
        // current_max and update result if
        // count becomes more
        else
        {
            current_max++; // increase count
            max_so_far = Math.Max(current_max, 
                                max_so_far);
        }
    }
  
    return max_so_far;
}
  
// Driver code
public static void Main () 
{
    int []arr = { 1, 0, 2, 4, 3, 8, 9 };
      int n = arr.Length;
      
    Console.WriteLine(countMaxContiguous(arr, n));
}
}
  
// This code is contributed
// by inder_verma

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to count maximum 
// contiguous even numbers 
  
// Function to count maximum 
// contiguous even numbers 
function countMaxContiguous(&$arr, $n
    $max_so_far=0;
    for ($i = 0; $i < $n; $i++) 
    
  
        // If current element is not 
        // even reset current_max 
        if ($arr[$i] % 2 != 0) 
            $current_max = 0; 
  
        // If even element is found,  
        // increment current_max and 
        // update result if count 
        // becomes more 
        else 
        
            // increase count 
            $current_max = $current_max + 1; 
            $max_so_far = max($current_max
                              $max_so_far); 
        
    
  
    return $max_so_far
  
// Driver code 
$arr = array(1, 0, 2, 4, 3, 8, 9 ); 
$n = sizeof($arr); 
  
echo countMaxContiguous($arr, $n); 
  
// This code is contributed
// by Shivi_Aggarwal
?>

chevron_right


Output:

3


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.