Longest alternative parity subsequence
We are given an array a of size N. The task is to print the length of the longest alternative odd/even or even/odd subsequence.
Examples:
Input: a[] = { 13, 16, 8, 9, 32, 10 }
Output: 4
{13, 16, 9, 10} or any other subsequence of length 4 can be the answer.
Input: a[] = {1, 2, 3, 3, 9}
Output: 3
Approach: The answer to the longest alternative parity subsequence will be either [odd, even, odd, even, …..] or [even, odd, even, odd, ….] sequence. Hence iterate in the array and first find the longest odd/even subsequence, and then the most extended even/odd sequence. The steps to find the longest subsequence is:
- Iterate and find the next odd number and increase the length.
- Iterate and find the next odd number and increase the length.
- Repeat steps 1 and steps 2 alternatively starting from steps 1 till the end to find the longest odd/even subsequence.
- Repeat steps 1 and steps 2 alternatively starting from steps 2 till the end to find the longest even/odd subsequence.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int longestAlternativeSequence( int a[], int n)
{
int maxi1 = 0;
int f1 = 0;
for ( int i = 0; i < n; i++) {
if (!f1) {
if (a[i] % 2) {
f1 = 1;
maxi1++;
}
}
else {
if (a[i] % 2 == 0) {
maxi1++;
f1 = 0;
}
}
}
int maxi2 = 0;
int f2 = 0;
for ( int i = 0; i < n; i++) {
if (f2) {
if (a[i] % 2) {
f2 = 1;
maxi2++;
}
}
else {
if (a[i] % 2 == 0) {
maxi2++;
f2 = 0;
}
}
}
return max(maxi1, maxi2);
}
int main()
{
int a[] = { 13, 16, 8, 9, 32, 10 };
int n = sizeof (a) / sizeof (a[0]);
cout << longestAlternativeSequence(a, n);
}
|
Java
class GFG
{
static int longestAlternativeSequence( int a[], int n)
{
int maxi1 = 0 ;
int f1 = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (f1 % 2 != 0 )
{
if (a[i] % 2 == 1 )
{
f1 = 1 ;
maxi1++;
}
}
else
{
if (a[i] % 2 == 0 )
{
maxi1++;
f1 = 0 ;
}
}
}
int maxi2 = 0 ;
int f2 = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (f2 % 2 != 0 )
{
if (a[i] % 2 == 1 )
{
f2 = 1 ;
maxi2++;
}
}
else
{
if (a[i] % 2 == 0 )
{
maxi2++;
f2 = 0 ;
}
}
}
return Math.max(maxi1, maxi2);
}
public static void main(String[] args)
{
int a[] = { 13 , 16 , 8 , 9 , 32 , 10 };
int n = a.length;
System.out.println(longestAlternativeSequence(a, n));
}
}
|
Python3
def longestAlternativeSequence(a, n):
maxi1 = 0
f1 = 0
for i in range (n):
if (f1 = = 0 ):
if (a[i] % 2 ):
f1 = 1
maxi1 + = 1
else :
if (a[i] % 2 = = 0 ):
maxi1 + = 1
f1 = 0
maxi2 = 0
f2 = 0
for i in range (n):
if (f2):
if (a[i] % 2 ):
f2 = 1
maxi2 + = 1
else :
if (a[i] % 2 = = 0 ):
maxi2 + = 1
f2 = 0
return max (maxi1, maxi2)
a = [ 13 , 16 , 8 , 9 , 32 , 10 ]
n = len (a)
print (longestAlternativeSequence(a, n))
|
C#
using System;
class GFG
{
static int longestAlternativeSequence( int []a,
int n)
{
int maxi1 = 0;
int f1 = 0;
for ( int i = 0; i < n; i++)
{
if (f1 != 0)
{
if (a[i] % 2 == 0)
{
f1 = 1;
maxi1++;
}
}
else
{
if (a[i] % 2 == 0)
{
maxi1++;
f1 = 0;
}
}
}
int maxi2 = 0;
int f2 = 0;
for ( int i = 0; i < n; i++)
{
if (f2 == 0)
{
if (a[i] % 2 == 0)
{
f2 = 1;
maxi2++;
}
}
else
{
if (a[i] % 2 == 0)
{
maxi2++;
f2 = 0;
}
}
}
return Math.Max(maxi1, maxi2);
}
public static void Main()
{
int []a = { 13, 16, 8, 9, 32, 10 };
int n = a.Length;
Console.Write(longestAlternativeSequence(a, n));
}
}
|
Javascript
<script>
function longestAlternativeSequence(a, n)
{
let maxi1 = 0;
let f1 = 0;
for (let i = 0; i < n; i++) {
if (!f1) {
if (a[i] % 2) {
f1 = 1;
maxi1++;
}
}
else {
if (a[i] % 2 == 0) {
maxi1++;
f1 = 0;
}
}
}
let maxi2 = 0;
let f2 = 0;
for (let i = 0; i < n; i++) {
if (f2) {
if (a[i] % 2) {
f2 = 1;
maxi2++;
}
}
else {
if (a[i] % 2 == 0) {
maxi2++;
f2 = 0;
}
}
}
return Math.max(maxi1, maxi2);
}
let a = [ 13, 16, 8, 9, 32, 10 ];
let n = a.length;
document.write(longestAlternativeSequence(a, n));
</script>
|
Time complexity – O(n), since there runs a loop from 0 to (n – 1).
Auxiliary Space – O(1), since no extra space has been taken.
Last Updated :
15 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...