Length of the longest Subarray with only Even Elements
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++; } else { // If element are not even in continuous // fashion, Reinitialize the count ans = max(ans, 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++; } else { // If element are not even in continuous // fashion. Reinitialize the count ans = Math.max(ans, 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 else: # If element are not even in continuous # fashion, Reinitialize the count ans = max(ans,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++; } else { // If element are not even in // continuous fashion, // Reinitialize the count ans = Math.Max(ans, 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++; } else { // If element are not even in // continuous fashion, // Reinitialize the count $ans = max($ans, $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: ![]()
Recommended Posts:
- Length of the longest alternating subarray
- Length of longest increasing circular subarray
- Longest Subarray having sum of elements atmost 'k'
- Longest subarray in which all elements are greater than K
- Longest subarray with elements divisible by k
- Longest subarray not having more than K distinct elements
- Length of the largest subarray with contiguous elements | Set 1
- Longest subarray with elements having equal modulo K
- Maximum length subarray with difference between adjacent elements as either 0 or 1
- Longest subarray such that adjacent elements have at least one common digit
- Longest subarray such that the difference of max and min is at-most one
- Longest increasing subarray
- Longest subarray with sum divisible by k
- Longest subarray with only one value greater than k
- Longest subarray having maximum sum
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.



