Find the count of maximum contiguous Even numbers
Last Updated :
06 Apr, 2023
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
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
arr = [ 1 , 0 , 2 , 4 , 3 , 8 , 9 ]
maxEven = maxContiguousEven(arr)
print (maxEven)
|
C#
using System;
public 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);
Console.WriteLine(maxEven);
}
}
|
Javascript
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;
}
let arr = [1, 0, 2, 4, 3, 8, 9];
let maxEven = maxContiguousEven(arr);
console.log(maxEven);
|
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++
#include <iostream>
using namespace std;
int countMaxContiguous( int arr[], int n)
{
int current_max = 0, max_so_far = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] % 2 != 0)
current_max = 0;
else {
current_max++;
max_so_far = max(current_max, max_so_far);
}
}
return max_so_far;
}
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
import java.io.*;
class GFG
{
static int countMaxContiguous( int arr[], int n)
{
int current_max = 0 , max_so_far = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] % 2 != 0 )
current_max = 0 ;
else
{
current_max++;
max_so_far = Math.max(current_max,
max_so_far);
}
}
return max_so_far;
}
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));
}
}
|
Python 3
def countMaxContiguous(arr, n):
current_max = 0
max_so_far = 0
for i in range (n) :
if (arr[i] % 2 ! = 0 ):
current_max = 0
else :
current_max + = 1
max_so_far = max (current_max,
max_so_far)
return max_so_far
if __name__ = = "__main__" :
arr = [ 1 , 0 , 2 , 4 , 3 , 8 , 9 ]
n = len (arr)
print (countMaxContiguous(arr, n))
|
C#
using System;
class GFG
{
static int countMaxContiguous( int []arr, int n)
{
int current_max = 0, max_so_far = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] % 2 != 0)
current_max = 0;
else
{
current_max++;
max_so_far = Math.Max(current_max,
max_so_far);
}
}
return max_so_far;
}
public static void Main ()
{
int []arr = { 1, 0, 2, 4, 3, 8, 9 };
int n = arr.Length;
Console.WriteLine(countMaxContiguous(arr, n));
}
}
|
PHP
<?php
function countMaxContiguous(& $arr , $n )
{
$max_so_far =0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $arr [ $i ] % 2 != 0)
$current_max = 0;
else
{
$current_max = $current_max + 1;
$max_so_far = max( $current_max ,
$max_so_far );
}
}
return $max_so_far ;
}
$arr = array (1, 0, 2, 4, 3, 8, 9 );
$n = sizeof( $arr );
echo countMaxContiguous( $arr , $n );
?>
|
Javascript
<script>
function countMaxContiguous(arr, n)
{
let current_max = 0, max_so_far = 0;
for (let i = 0; i < n; i++) {
if (arr[i] % 2 != 0)
current_max = 0;
else {
current_max++;
max_so_far = Math.max(current_max, max_so_far);
}
}
return max_so_far;
}
let arr = [ 1, 0, 2, 4, 3, 8, 9 ];
let n = arr.length;
document.write(countMaxContiguous(arr, n));
</script>
|
Time Complexity: O(N)
Space Complexity: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...