Open In App

Find the count of maximum contiguous Even numbers

Improve
Improve
Like Article
Like
Save
Share
Report

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} 

Brute Force Approach:

Brute force approach would be to generate all possible contiguous subarrays and check each subarray to find the one with the maximum even numbers. This can be done using two nested loops to generate the subarrays and another loop to count the number of even numbers in each subarray.

Implementation of the above approach:

C++




#include <bits/stdc++.h>
using namespace std;
 
int maxContiguousEven(int arr[], int n) {
    int maxEven = 0;
    for (int i = 0; i < n; i++) {
        int currEven = 0;
        for (int j = i; j < n; j++) {
            if (arr[j] % 2 == 0) {
                currEven++;
            } else {
                break;
            }
        }
        maxEven = max(maxEven, currEven);
    }
    return maxEven;
}
 
int main() {
    int arr[] = {1, 0, 2, 4, 3, 8, 9};
    int n = sizeof(arr) / sizeof(arr[0]);
    int maxEven = maxContiguousEven(arr, n);
    cout << maxEven << endl;
    return 0;
}


Java




import java.util.*;
 
class GFG {
    public static int maxContiguousEven(int[] arr, int n)
    {
        int maxEven = 0;
        for (int i = 0; i < n; i++) {
            int currEven = 0;
            for (int j = i; j < n; j++) {
                if (arr[j] % 2 == 0) {
                    currEven++;
                }
                else {
                    break;
                }
            }
            maxEven = Math.max(maxEven, currEven);
        }
        return maxEven;
    }
 
    public static void main(String[] args)
    {
        int[] arr = { 1, 0, 2, 4, 3, 8, 9 };
        int n = arr.length;
        int maxEven = maxContiguousEven(arr, n);
        System.out.println(maxEven);
    }
}


Python3




# Function to find the maximum number of contiguous even elements in an array
def maxContiguousEven(arr):
    maxEven = 0
    for i in range(len(arr)):
        currEven = 0
        for j in range(i, len(arr)):
            if arr[j] % 2 == 0:
                currEven += 1
            else:
                break
        maxEven = max(maxEven, currEven)
    return maxEven
 
# Example usage
arr = [1, 0, 2, 4, 3, 8, 9]
# function call
maxEven = maxContiguousEven(arr)
print(maxEven)


C#




// C# code addition
using System;
 
public class GFG
{
  // A function which returns maximum continuous even numbers count
  public static int MaxContiguousEven(int[] arr, int n)
  {
    // stores the result
    int maxEven = 0;
    for (int i = 0; i < n; i++)
    {
      // stores even numbers for each subarray starting with "i"
      int currEven = 0;
      for (int j = i; j < n; j++)
      {
        // if found even then increment even count
        if (arr[j] % 2 == 0)
        {
          currEven++;
        }
        else
        {
          break;
        }
      }
 
      // update thre maximum even numbers count with current even numbers count.
      maxEven = Math.Max(maxEven, currEven);
    }
    return maxEven;
  }
 
  // Driver code
  public static void Main(string[] args)
  {
    int[] arr = { 1, 0, 2, 4, 3, 8, 9 };
    int n = arr.Length;
    int maxEven = MaxContiguousEven(arr, n);
    Console.WriteLine(maxEven);
  }
}
 
// The code is contributed by Arushi Goel.


Javascript




// Function to find the maximum number of contiguous even elements in an array
function maxContiguousEven(arr) {
    let maxEven = 0;
    for (let i = 0; i < arr.length; i++) {
        let currEven = 0;
        for (let j = i; j < arr.length; j++) {
            if (arr[j] % 2 == 0) {
                currEven++;
            } else {
                break;
            }
        }
        maxEven = Math.max(maxEven, currEven);
    }
    return maxEven;
}
 
// Example usage
let arr = [1, 0, 2, 4, 3, 8, 9];
let maxEven = maxContiguousEven(arr);
console.log(maxEven);


Output

3

Time Complexity: O(n^2)

Auxiliary Space: O(1)

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.

Implementation:

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

Time Complexity: O(N)

Space Complexity: O(1)



Last Updated : 06 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads