Skip to content
Related Articles

Related Articles

Find the count of maximum contiguous Even numbers
  • Difficulty Level : Easy
  • Last Updated : 03 Mar, 2021

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




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

Java




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

Python 3




# 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

C#




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

PHP




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

Javascript




<script>
 
// Javascript program to count maximum
// contiguous even numbers
 
// Function to count maximum
// contiguous even numbers
function countMaxContiguous(arr, n)
{
    let current_max = 0, max_so_far = 0;
 
    for (let 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
 
    let arr = [ 1, 0, 2, 4, 3, 8, 9 ];
 
    let n = arr.length;
 
    document.write(countMaxContiguous(arr, n));
 
 
// This code is contributed by Mayank Tyagi
 
</script>
Output: 
3

 

competitive-programming-img




My Personal Notes arrow_drop_up
Recommended Articles
Page :