Maximum length of subarray such that sum of the subarray is even
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 : 5
Explanation: 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
Naive Approach
The idea is to find all subarrays and then find those subarrays whose sum of elements are even. After that choose the longest length of those subarrays.
Steps to implement-
- Declare a variable ans with value 0 to store the final answer
- Run two loops to find all subarrays
- Find the sum of all elements of the subarray
- When the sum of all elements of the subarray is even
- Then update ans as the maximum of ans and the length of that subarray
Code-
C++
#include <bits/stdc++.h>
using namespace std;
int maxLength( int arr[], int N)
{
int ans=0;
for ( int i=0;i<N;i++){
int length=0;
for ( int j=i;j<N;j++){
length++;
bool val= false ;
int sum=0;
for ( int k=i;k<=j;k++){
sum+=arr[k];
}
if (sum%2==0){
ans=max(ans,length);
}
}
}
return ans;
}
int main()
{
int arr[] = { 1, 2, 3, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << maxLength(arr, N) << "\n" ;
return 0;
}
|
Java
import java.util.*;
public class Main {
static int maxLength( int [] arr, int N) {
int ans = 0 ;
for ( int i = 0 ; i < N; i++) {
int length = 0 ;
for ( int j = i; j < N; j++) {
length++;
boolean val = false ;
int sum = 0 ;
for ( int k = i; k <= j; k++) {
sum += arr[k];
}
if (sum % 2 == 0 ) {
ans = Math.max(ans, length);
}
}
}
return ans;
}
public static void main(String[] args) {
int [] arr = { 1 , 2 , 3 , 2 };
int N = arr.length;
System.out.println(maxLength(arr, N));
}
}
|
Python3
def maxLength(arr):
ans = 0
for i in range ( len (arr)):
length = 0
for j in range (i, len (arr)):
length + = 1
val = False
subarray_sum = 0
for k in range (i, j + 1 ):
subarray_sum + = arr[k]
if subarray_sum % 2 = = 0 :
ans = max (ans, length)
return ans
arr = [ 1 , 2 , 3 , 2 ]
print (maxLength(arr))
|
C#
using System;
public class GFG
{
public static int MaxLength( int [] arr, int N)
{
int ans = 0;
for ( int i = 0; i < N; i++)
{
int length = 0;
for ( int j = i; j < N; j++)
{
length++;
int sum = 0;
for ( int k = i; k <= j; k++)
{
sum += arr[k];
}
if (sum % 2 == 0)
{
ans = Math.Max(ans, length);
}
}
}
return ans;
}
public static void Main( string [] args)
{
int [] arr = { 1, 2, 3, 2 };
int N = arr.Length;
Console.WriteLine(MaxLength(arr, N));
}
}
|
Javascript
function MaxLength(arr, N) {
let ans = 0;
for (let i = 0; i < N; i++) {
let length = 0;
for (let j = i; j < N; j++) {
length++;
let sum = 0;
for (let k = i; k <= j; k++) {
sum += arr[k];
}
if (sum % 2 === 0) {
ans = Math.max(ans, length);
}
}
}
return ans;
}
const arr = [1, 2, 3, 2];
const N = arr.length;
console.log(MaxLength(arr, N));
|
Time Complexity: O(N3), because of two nested loops to find all subarray and third loop is to find the sum of all elements of subarray
Auxiliary Space: O(1), because no extra space has been used
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++
#include <bits/stdc++.h>
using namespace std;
int maxLength( int a[], int n)
{
int sum = 0, len = 0;
for ( int i = 0; i < n; i++)
sum += a[i];
if (sum % 2 == 0)
return n;
for ( int i = 0; i < n; i++) {
if (a[i] % 2 == 1)
len = max(len, max(n - i - 1, i));
}
return len;
}
int main()
{
int a[] = { 1, 2, 3, 2 };
int n = sizeof (a) / sizeof (a[0]);
cout << maxLength(a, n) << "\n" ;
return 0;
}
|
Java
class GFG
{
static int maxLength( int a[], int n)
{
int sum = 0 , len = 0 ;
for ( int i = 0 ; i < n; i++)
{
sum += a[i];
}
if (sum % 2 == 0 )
{
return n;
}
for ( int i = 0 ; i < n; i++)
{
if (a[i] % 2 == 1 )
{
len = Math.max(len, Math.max(n - i - 1 , i));
}
}
return len;
}
public static void main(String[] args)
{
int a[] = { 1 , 2 , 3 , 2 };
int n = a.length;
System.out.println(maxLength(a, n));
}
}
|
Python
def maxLength(a, n):
Sum = 0
Len = 0
for i in range (n):
Sum + = a[i]
if ( Sum % 2 = = 0 ):
return n
for i in range (n):
if (a[i] % 2 = = 1 ):
Len = max ( Len , max (n - i - 1 , i))
return Len
a = [ 1 , 2 , 3 , 2 ]
n = len (a)
print (maxLength(a, n))
|
C#
using System;
class GFG
{
static int maxLength( int []a, int n)
{
int sum = 0, len = 0;
for ( int i = 0; i < n; i++)
{
sum += a[i];
}
if (sum % 2 == 0)
{
return n;
}
for ( int i = 0; i < n; i++)
{
if (a[i] % 2 == 1)
{
len = Math.Max(len, Math.Max(n - i - 1, i));
}
}
return len;
}
static public void Main ()
{
int []a = {1, 2, 3, 2};
int n = a.Length;
Console.WriteLine(maxLength(a, n));
}
}
|
Javascript
<script>
function maxLength(a, n)
{
let sum = 0, len = 0;
for (let i = 0; i < n; i++)
sum += a[i];
if (sum % 2 == 0)
return n;
for (let i = 0; i < n; i++) {
if (a[i] % 2 == 1)
len = Math.max(len, Math.max(n - i - 1, i));
}
return len;
}
let a = [ 1, 2, 3, 2 ];
let n = a.length;
document.write(maxLength(a, n) + "<br>" );
</script>
|
PHP
<?php
function maxLength( $a , $n )
{
$sum = 0;
$len = 0;
for ( $i = 0; $i < $n ; $i ++)
$sum += $a [ $i ];
if ( $sum % 2 == 0)
return $n ;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $a [ $i ] % 2 == 1)
$len = max( $len , $max ( $n - $i - 1, $i ));
}
return $len ;
}
$a = array (1, 2, 3, 2 );
$n = count ( $a );
echo maxLength( $a , $n ) , "\n" ;
?>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
14 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...