Given an array of N elements. The task is to find the length of the longest subarray such that sum of the subarray is even.

**Examples:**

Input :N = 6, arr[] = {1, 2, 3, 2, 1, 4}Output :5Explanation:In the example the subarray in range [2, 6] has sum 12 which is even, so the length is 5.Input :N = 4, arr[] = {1, 2, 3, 2}Output :4

**Approach: ** First check if the total sum of the array is even. If the total sum of the array is even then the answer will be N.

If the total sum of the array is not even, means it is ODD. So, the idea is to find an odd element from the array such that excluding that element and comparing the length of both parts of the array we can obtain the max length of the subarray with even sum.

It is obvious that the subarray with even sum will exist in range [1, x) or (x, N],

where 1 <= x <= N, and arr[x] is ODD.

Below is the implementation of above approach:

## C++

`// C++ implementation of the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find length of the longest ` `// subarray such that sum of the ` `// subarray is even ` `int` `maxLength(` `int` `a[], ` `int` `n) ` `{ ` ` ` `int` `sum = 0, len = 0; ` ` ` ` ` `// Check if sum of complete array is even ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `sum += a[i]; ` ` ` ` ` `if` `(sum % 2 == 0) ` `// total sum is already even ` ` ` `return` `n; ` ` ` ` ` `// Find an index i such the a[i] is odd ` ` ` `// and compare length of both halfs excluding ` ` ` `// a[i] to find max length subarray ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(a[i] % 2 == 1) ` ` ` `len = max(len, max(n - i - 1, i)); ` ` ` `} ` ` ` ` ` `return` `len; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 1, 2, 3, 2 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` ` ` `cout << maxLength(a, n) << ` `"\n"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find length of the longest ` ` ` `// subarray such that sum of the ` ` ` `// subarray is even ` ` ` `static` `int` `maxLength(` `int` `a[], ` `int` `n) ` ` ` `{ ` ` ` `int` `sum = ` `0` `, len = ` `0` `; ` ` ` ` ` `// Check if sum of complete array is even ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `sum += a[i]; ` ` ` `} ` ` ` ` ` `if` `(sum % ` `2` `== ` `0` `) ` `// total sum is already even ` ` ` `{ ` ` ` `return` `n; ` ` ` `} ` ` ` ` ` `// Find an index i such the a[i] is odd ` ` ` `// and compare length of both halfs excluding ` ` ` `// a[i] to find max length subarray ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `if` `(a[i] % ` `2` `== ` `1` `) ` ` ` `{ ` ` ` `len = Math.max(len, Math.max(n - i - ` `1` `, i)); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `len; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `a[] = {` `1` `, ` `2` `, ` `3` `, ` `2` `}; ` ` ` `int` `n = a.length; ` ` ` `System.out.println(maxLength(a, n)); ` ` ` ` ` `} ` `} ` ` ` `// This code has been contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python

`# Python3 implementation of the above approach ` ` ` `# Function to find Length of the longest ` `# subarray such that Sum of the ` `# subarray is even ` `def` `maxLength(a, n): ` ` ` ` ` `Sum` `=` `0` ` ` `Len` `=` `0` ` ` ` ` `# Check if Sum of complete array is even ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `Sum` `+` `=` `a[i] ` ` ` ` ` `if` `(` `Sum` `%` `2` `=` `=` `0` `): ` `# total Sum is already even ` ` ` `return` `n ` ` ` ` ` `# Find an index i such the a[i] is odd ` ` ` `# and compare Length of both halfs excluding ` ` ` `# a[i] to find max Length subarray ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `if` `(a[i] ` `%` `2` `=` `=` `1` `): ` ` ` `Len` `=` `max` `(` `Len` `, ` `max` `(n ` `-` `i ` `-` `1` `, i)) ` ` ` ` ` `return` `Len` ` ` `# Driver Code ` ` ` `a` `=` `[` `1` `, ` `2` `, ` `3` `, ` `2` `] ` `n ` `=` `len` `(a) ` ` ` `print` `(maxLength(a, n)) ` ` ` `# This code is contributed by mohit kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find length of the longest ` ` ` `// subarray such that sum of the ` ` ` `// subarray is even ` ` ` `static` `int` `maxLength(` `int` `[]a, ` `int` `n) ` ` ` `{ ` ` ` `int` `sum = 0, len = 0; ` ` ` ` ` `// Check if sum of complete array is even ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `sum += a[i]; ` ` ` `} ` ` ` ` ` `if` `(sum % 2 == 0) ` `// total sum is already even ` ` ` `{ ` ` ` `return` `n; ` ` ` `} ` ` ` ` ` `// Find an index i such the a[i] is odd ` ` ` `// and compare length of both halfs excluding ` ` ` `// a[i] to find max length subarray ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `if` `(a[i] % 2 == 1) ` ` ` `{ ` ` ` `len = Math.Max(len, Math.Max(n - i - 1, i)); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `len; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `int` `[]a = {1, 2, 3, 2}; ` ` ` `int` `n = a.Length; ` ` ` `Console.WriteLine(maxLength(a, n)); ` ` ` ` ` `} ` `} ` ` ` `// This code has been contributed by ajit. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `//PHP implementation of the above approach ` ` ` `// Function to find length of the longest ` `// subarray such that sum of the ` `// subarray is even ` `function` `maxLength(` `$a` `, ` `$n` `) ` `{ ` ` ` `$sum` `= 0; ` ` ` `$len` `= 0; ` ` ` ` ` `// Check if sum of complete array is even ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `$sum` `+= ` `$a` `[` `$i` `]; ` ` ` ` ` `if` `(` `$sum` `% 2 == 0) ` `// total sum is already even ` ` ` `return` `$n` `; ` ` ` ` ` `// Find an index i such the a[i] is odd ` ` ` `// and compare length of both halfs excluding ` ` ` `// a[i] to find max length subarray ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `{ ` ` ` `if` `(` `$a` `[` `$i` `] % 2 == 1) ` ` ` `$len` `= max(` `$len` `, ` `$max` `(` `$n` `- ` `$i` `- 1, ` `$i` `)); ` ` ` `} ` ` ` ` ` `return` `$len` `; ` `} ` ` ` `// Driver Code ` `$a` `= ` `array` `(1, 2, 3, 2 ); ` `$n` `= ` `count` `(` `$a` `); ` ` ` `echo` `maxLength(` `$a` `, ` `$n` `) , ` `"\n"` `; ` ` ` ` ` `// This code is contributed by akt_mit. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximum sum of even indexed elements obtained by right shift on an even sized subarray
- Maximum length of subarray such that all elements are equal in the subarray
- Maximum sum subarray of even length
- Maximum length L such that the sum of all subarrays of length L is less than K
- Length of longest subarray of length at least 2 with maximum GCD
- First subarray having sum at least half the maximum sum of any subarray of size K
- Rearrange array such that all even-indexed elements in the Array is even
- Number of ways to cut a stick of length N into in even length at most K units long pieces
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Length of the longest Subarray with only Even Elements
- Length of longest Subarray with equal number of odd and even elements
- Length of the longest alternating even odd subarray
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Maximum sum subarray such that start and end values are same
- Maximum subarray size, such that all subarrays of that size have sum less than k
- Split array to three subarrays such that sum of first and third subarray is equal and maximum
- Maximum sum subarray having sum less than or equal to given sum
- Maximum sum subarray having sum less than or equal to given sum using Set
- Count ways to express even number ānā as sum of even integers
- Sum of even numbers at even position

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.