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++
// 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 ?> |
3
Recommended Posts:
- Maximum no. of contiguous Prime Numbers in an array
- Maximum count of equal numbers in an array after performing given operations
- Maximum absolute difference between sum of two contiguous sub-arrays
- K maximum sums of overlapping contiguous sub-arrays
- K maximum sums of non-overlapping contiguous sub-arrays
- Given count of digits 1, 2, 3, 4, find the maximum sum possible
- Pick maximum sum M elements such that contiguous repetitions do not exceed K
- Count the number of ways to divide an array into three contiguous parts having equal sum
- Find the maximum sum of digits of the product of two numbers
- Program to find the maximum difference between the index of any two different numbers
- Find all numbers that divide maximum array elements
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- Maximum number of contiguous array elements with same number of set bits
- Find all unique pairs of maximum and second maximum elements over all sub-arrays in O(NlogN)
- Count numbers which are divisible by all the numbers from 2 to 10
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.



