Given an array arr[]. The task is to find the length of the longest subarray of arr[] such that it contains only even elements.
Examples:
Input : arr[] = { 5, 2, 4, 7 } Output : Length = 2 subArr[] = {2, 4} Input : arr[] = {9, 8, 5, 4, 4, 4, 2, 4, 1} Output : Length 5 subArr[] = {4, 4, 4, 2, 4}
The idea is to observe that the largest subarray with only even elements is the maximum number of contiguous even elements in the array. Therefore, the task now reduces to find the maximum number of contiguous even elements in the array.
To do this, traverse the array using two variables, ans and current_count. The variable ans stores the final answer and current_count stores the length of subarray with only even numbers.
Now whenever an even element is found, keep incrementing the current_count and whenever an ODD element is found take the maximum of ans and current_count and reset current_count to zero.
At the end, ans will store the length of largest subarray with only even elements.
Below is the implementation of the above approach:
C++
// C++ Program to find the Length of the // largest Subarray with only even elements #include <cmath> #include <iostream> using namespace std; // Function to find the Length of the // largest Subarray with only even elements int maxEvenSubarray( int array[], int N) { int ans = 0; int count = 0; // Iterate the loop for ( int i = 0; i < N; i++) { // Check whether the element is // even in continuous fashion if (array[i] % 2 == 0) { count++; ans = max(ans, count); } else { // If element are not even in continuous // fashion, Reinitialize the count count = 0; } } // Check for the last element in the array ans = max(ans, count); return ans; } // Driver Code int main() { int arr[] = { 9, 8, 5, 4, 4, 4, 2, 4, 1 }; int N = sizeof (arr) / sizeof (arr[0]); cout << maxEvenSubarray(arr, N); return 0; } |
Java
// Java Program to find the Length of the longest // Subarray with only Even Elements public class GFG { // Function to find the Length of the longest // Subarray with only Even Elements static int maxEvenSubarray( int array[], int N) { int ans = 0 ; int count = 0 ; // Iterate the loop for ( int i = 0 ; i < array.length; i++) { // Check whether the element is // even in continuous fashion if (array[i] % 2 == 0 ) { count++; ans = Math.max(ans, count); } else { // If element are not even in continuous // fashion. Reinitialize the count count = 0 ; } } // Check for the last element in the array ans = Math.max(ans, count); return ans; } // Driver Code public static void main(String args[]) { int arr[] = { 9 , 8 , 5 , 4 , 4 , 4 , 2 , 4 , 1 }; int N = arr.length; System.out.println(maxEvenSubarray(arr, N)); } } |
Python3
# Python3 Program to find the Length of the # largest Subarray with only even elements # Function to find the Length of the # largest Subarray with only even elements def maxEvenSubarray(array,N): ans = 0 count = 0 # Iterate the loop for i in range ( 0 ,N): # Check whether the element is # even in continuous fashion if array[i] % 2 = = 0 : count + = 1 ans = max (ans,count) else : # If element are not even in continuous # fashion, Reinitialize the count count = 0 # Check for the last element in the array ans = max (ans,count) return ans # Driver code if __name__ = = '__main__' : arr = [ 9 , 8 , 5 , 4 , 4 , 4 , 2 , 4 , 1 ] N = len (arr) print (maxEvenSubarray(arr,N)) # This article is contributed by Shrikant13 |
C#
// C# Program to find the Length // of the largest Subarray with // only even elements using System; class GFG { // Function to find the Length // of the largest Subarray with // only even elements static int maxEvenSubarray( int []array, int N) { int ans = 0; int count = 0; // Iterate the loop for ( int i = 0; i < N; i++) { // Check whether the element is // even in continuous fashion if (array[i] % 2 == 0) { count++; ans = Math.Max(ans, count); } else { // If element are not even in // continuous fashion, // Reinitialize the count count = 0; } } // Check for the last // element in the array ans = Math.Max(ans, count); return ans; } // Driver Code public static void Main() { int []arr = { 9, 8, 5, 4, 4, 4, 2, 4, 1 }; int N = arr.Length; Console.WriteLine(maxEvenSubarray(arr, N)); } } // This code is contributed by ihritik |
PHP
<?php // PHP Program to find the Length // of the largest Subarray with // only even elements // Function to find the Length // of the largest Subarray with // only even elements function maxEvenSubarray( $array , $N ) { $ans = 0; $count = 0; // Iterate the loop for ( $i = 0; $i < $N ; $i ++) { // Check whether the element is // even in continuous fashion if ( $array [ $i ] % 2 == 0) { $count ++; $ans = max( $ans , $count ); } else { // If element are not even in // continuous fashion, // Reinitialize the count $count = 0; } } // Check for the last // element in the array $ans = max( $ans , $count ); return $ans ; } // Driver Code $arr = array ( 9, 8, 5, 4, 4, 4, 2, 4, 1 ); $N = sizeof( $arr ); echo maxEvenSubarray( $arr , $N ); // This code is contributed by ihritik ?> |
5
Time Complexity: