Length of the longest alternating even odd subarray
Last Updated :
06 Apr, 2023
Given an array a[] of N integers, the task is to find the length of the longest Alternating Even Odd subarray present in the array.
Examples:
Input: a[] = {1, 2, 3, 4, 5, 7, 9}
Output: 5
Explanation:
The subarray {1, 2, 3, 4, 5} has alternating even and odd elements.
Input: a[] = {1, 3, 5}
Output: 0
Explanation:
There is no such alternating sequence possible.
Naive approach:
The idea is to consider every subarray and find the length of even and odd subarrays.
Follow the steps below to solve the problem:
- Iterate for every subarray from i = 0
- Make a nested loop, iterate from j = i + 1
- Now, check if a[j – 1] is even and a[j] is odd or a[j – 1] is odd and a[j] is even then increment count
- Maintain an answer variable which calculates max count so far
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int longestEvenOddSubarray( int a[], int n)
{
int ans = 1;
for ( int i = 0; i < n; i++) {
int cnt = 1;
for ( int j = i + 1; j < n; j++) {
if ((a[j - 1] % 2 == 0 && a[j] % 2 != 0)
|| (a[j - 1] % 2 != 0 && a[j] % 2 == 0))
cnt++;
else
break ;
}
ans = max(ans, cnt);
}
if (ans == 1)
return 0;
return ans;
}
int main()
{
int a[] = { 1, 2, 3, 4, 5, 7, 8 };
int n = sizeof (a) / sizeof (a[0]);
cout << longestEvenOddSubarray(a, n);
return 0;
}
|
Java
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class GfG {
static int longestEvenOddSubarray(ArrayList<Integer> a,
int n)
{
int ans = 1 ;
for ( int i = 0 ; i < n; i++) {
int cnt = 1 ;
for ( int j = i + 1 ; j < n; j++) {
if ((a.get(j - 1 ) % 2 == 0
&& a.get(j) % 2 != 0 )
|| (a.get(j - 1 ) % 2 != 0
&& a.get(j) % 2 == 0 ))
cnt++;
else
break ;
}
ans = Math.max(ans, cnt);
}
if (ans == 1 )
return 0 ;
return ans;
}
public static void main(String args[])
{
ArrayList<Integer> a = new ArrayList<Integer>(
List.of( 1 , 2 , 3 , 4 , 5 , 7 , 8 ));
int n = a.size();
System.out.println(longestEvenOddSubarray(a, n));
}
}
|
Python3
import math
def longestEvenOddSubarray(a, n):
ans = 1
for i in range (n):
cnt = 1
for j in range (i + 1 , n):
if ((a[j - 1 ] % 2 = = 0 and a[j] % 2 ! = 0 )
or (a[j - 1 ] % 2 ! = 0 and a[j] % 2 = = 0 )):
cnt = cnt + 1
else :
break
ans = max (ans, cnt)
if (ans = = 1 ):
return 0
return ans
a = [ 1 , 2 , 3 , 4 , 5 , 7 , 8 ]
n = len (a)
print (longestEvenOddSubarray(a, n))
|
C#
using System;
using System.Collections.Generic;
public class GfG {
static int longestEvenOddSubarray(List< int > a, int n)
{
int ans = 1;
for ( int i = 0; i < n; i++) {
int cnt = 1;
for ( int j = i + 1; j < n; j++) {
if ((a[j - 1] % 2 == 0 && a[j] % 2 != 0)
|| (a[j - 1] % 2 != 0 && a[j] % 2 == 0))
cnt++;
else
break ;
}
ans = Math.Max(ans, cnt);
}
if (ans == 1)
return 0;
return ans;
}
public static void Main( string [] args)
{
List< int > a = new List< int >{ 1, 2, 3, 4, 5, 7, 8 };
int n = a.Count;
Console.WriteLine(longestEvenOddSubarray(a, n));
}
}
|
Javascript
<script>
function longestEvenOddSubarray(a, n)
{
let ans = 1;
for (let i = 0; i < n ; i++) {
let cnt = 1;
for (let j = i + 1; j < n; j++) {
if ((a[j - 1] % 2 == 0 && a[j] % 2 != 0)
|| (a[j - 1] % 2 != 0 && a[j] % 2 == 0))
cnt++;
else
break ;
}
ans = Math.max(ans, cnt);
}
if (ans == 1)
return 0;
return ans;
}
let a = [ 1, 2, 3, 4, 5, 7, 8 ];
let n = a.length;
document.write(longestEvenOddSubarray(a, n), "</br>" );
</script>
|
Time Complexity: O(N2), Iterating over every subarray therefore N2 are possible
Auxiliary Space: O(1)
Length of the longest alternating even odd subarray by Checking Parity of Sum:
Observe that the Sum of two even numbers is even, the Sum of two odd numbers is even but the sum of one even and one odd number is odd.
Follow the steps below to solve the problem:
- Initially initialize cnt a counter to store the length as 1.
- Iterate among the array elements, and check if consecutive elements have an odd sum.
- Increase the cnt by 1 if it has an odd sum.
- If it does not has an odd sum, then re-initialize cnt by 1.
- The function should return at least value 1 if there are elements in the array, because there can always be a subarray with length 1 which has either odd or even element.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int longestEvenOddSubarray( int arr[], int n)
{
int count = 1;
int maxcount = 1;
for ( int i = 0; i < n - 1; i++) {
if (arr[i] % 2 == 0 && arr[i + 1] % 2 != 0) {
count++;
}
if (arr[i] % 2 != 0 && arr[i + 1] % 2 == 0) {
count++;
}
if (arr[i] % 2 == 0 && arr[i + 1] % 2 == 0) {
count = 1;
}
if (arr[i] % 2 != 0 && arr[i + 1] % 2 != 0) {
count = 1;
}
maxcount = max(maxcount, count);
}
return maxcount;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 7, 8 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << longestEvenOddSubarray(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int longestEvenOddSubarray( int a[], int n)
{
int longest = 1 ;
int cnt = 1 ;
for ( int i = 0 ; i < n - 1 ; i++) {
if ((a[i] + a[i + 1 ]) % 2 == 1 ) {
cnt++;
}
else {
longest = Math.max(longest, cnt);
cnt = 1 ;
}
}
longest = Math.max(longest, cnt);
if (longest == 1 )
return 0 ;
return longest;
}
public static void main(String[] args)
{
int a[] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 };
int n = a.length;
System.out.println(longestEvenOddSubarray(a, n));
}
}
|
Python3
def longestEvenOddSubarray(arr, n):
longest = 1
cnt = 1
for i in range (n - 1 ):
if ((arr[i] + arr[i + 1 ]) % 2 = = 1 ):
cnt = cnt + 1
else :
longest = max (longest, cnt)
cnt = 1
if (longest = = 1 ):
return 0
return max (cnt, longest)
arr = [ 1 , 2 , 3 , 4 , 5 , 7 , 8 ]
n = len (arr)
print (longestEvenOddSubarray(arr, n))
|
C#
using System;
class GFG {
static int longestEvenOddSubarray( int [] a, int n)
{
int longest = 1;
int cnt = 1;
for ( int i = 0; i < n - 1; i++) {
if ((a[i] + a[i + 1]) % 2 == 1) {
cnt++;
}
else {
longest = Math.Max(longest, cnt);
cnt = 1;
}
}
if (longest == 1)
return 0;
return Math.Max(cnt, longest);
}
static void Main()
{
int [] a = { 1, 2, 3, 4, 5, 7, 8 };
int n = a.Length;
Console.WriteLine(longestEvenOddSubarray(a, n));
}
}
|
Javascript
<script>
function longestEvenOddSubarray(a, n)
{
let longest = 1;
let cnt = 1;
for (let i = 0; i < n - 1; i++) {
if ((a[i] + a[i + 1]) % 2 == 1)
{
cnt++;
}
else
{
longest = Math.max(longest, cnt);
cnt = 1;
}
}
if (longest == 1)
return 0;
return Math.max(cnt, longest);
}
let a = [ 1, 2, 3, 4, 5, 7, 8 ];
let n = a.length;
document.write(longestEvenOddSubarray(a, n));
</script>
|
Time Complexity: O(N), Traversing over the array one time.
Auxiliary Space: O(1)
Length of the longest alternating even odd subarray by Storing the previous element
By simply storing the nature of the previous element we encounter( odd or even) and comparing it with the next element.
Follow the steps below to solve the problem:
- Initialize a variable maxLength to 0, to keep the track of maximum length of the alternating subarray obtained.
- Initialize a variable currLen to 1 considering first element as the part of alternating subarray.
- Starting with element at index 1, compare every element with it’s previous. If there nature are different, increment the currLen variable.
- Otherwise, reset the currLen to 1 again so that, this current element is considered in new alternating subarray.
- Keep storing the max length of subarray in maxLength before resetting the currLen.
- Return the found max length of subarray.
Below is the implementation of above approach:
C++
#include <iostream>
using namespace std;
int maxEvenOdd( int arr[], int n)
{
if (n == 0)
return 0;
int maxLength = 0;
int currLen = 1;
for ( int i = 1; i < n; i++) {
if (arr[i] % 2 != arr[i-1] % 2)
currLen++;
else
{
maxLength = max(maxLength, currLen);
currLen = 1;
}
}
if (maxLength == 1)
return 0;
if (currLen > 1){
maxLength = max(maxLength , currLen);
}
return maxLength;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 3, 7, 2, 9, 4 };
int n = sizeof (arr) / sizeof ( int );
cout << "Length of longest subarray of even and odds "
"is : "
<< maxEvenOdd(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int maxEvenOdd( int [] arr, int n)
{
if (n == 0 )
return 0 ;
int maxLength = 0 ;
int prevOdd = arr[ 0 ] % 2 ;
int curLength = 1 ;
for ( int i = 1 ; i < n; i++)
{
if (arr[i] % 2 != prevOdd)
curLength++;
else
curLength = 1 ;
if (curLength > maxLength)
maxLength = curLength;
prevOdd = arr[i] % 2 ;
}
return maxLength;
}
static public void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 , 5 , 3 , 7 , 2 , 9 , 4 };
int n = arr.length;
System.out.print(
"Length of longest subarray of even and odds is : " );
System.out.print(maxEvenOdd(arr, n));
}
}
|
Python3
def maxEvenOdd(arr, n):
if (n = = 0 ):
return 0 ;
maxLength = 0 ;
prevOdd = arr[ 0 ] % 2 ;
curLength = 1 ;
for i in range ( 1 , n):
if (arr[i] % 2 ! = prevOdd):
curLength + = 1 ;
else :
curLength = 1 ;
if (curLength > maxLength):
maxLength = curLength;
prevOdd = arr[i] % 2 ;
return maxLength;
arr = [ 1 , 2 , 3 , 4 , 5 , 3 , 7 , 2 , 9 , 4 ];
n = len (arr);
print ( "Length of longest subarray of even and odds is :" , maxEvenOdd(arr, n));
|
C#
using System;
public class GFG {
public static int maxEvenOdd( int [] arr, int n)
{
if (n == 0)
return 0;
int maxLength = 0;
int prevOdd = arr[0] % 2;
int curLength = 1;
for ( int i = 1; i < n; i++)
{
if (arr[i] % 2 != prevOdd)
curLength++;
else
curLength = 1;
if (curLength > maxLength)
maxLength = curLength;
prevOdd = arr[i] % 2;
}
return maxLength;
}
static public void Main()
{
int [] arr = { 1, 2, 3, 4, 5, 3, 7, 2, 9, 4 };
int n = arr.Length;
Console.Write(
"Length of longest subarray of even and odds is : " );
Console.Write(maxEvenOdd(arr, n));
}
}
|
Javascript
function maxEvenOdd(arr, n)
{
if (n == 0)
return 0;
let maxLength = 0;
let prevOdd = arr[0] % 2;
let curLength = 1;
for ( var i = 1; i < n; i++) {
if (arr[i] % 2 != prevOdd)
curLength++;
else
curLength = 1;
if (curLength > maxLength)
maxLength = curLength;
prevOdd = arr[i] % 2;
}
return maxLength;
}
let arr = [ 1, 2, 3, 4, 5, 3, 7, 2, 9, 4 ];
let n = arr.length;
console.log( "Length of longest subarray of even and odds is : " +
maxEvenOdd(arr, n));
|
Output
Length of longest subarray of even and odds is : 5
Time Complexity: O(N), Since we need to iterate over the whole array once
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...