Longest alternative parity subsequence

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 longest 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 step 1 and step 2 alternatively starting from step 1 till the end to find the longest odd/even subsequence.
  • Repeat step 1 and step 2 alternatively starting from step 2 till the end to find the longest even/odd subsequence.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the length
// of the longest alternative parity
// subsequence
#include <iostream>
using namespace std;
  
// Function to find the longest
int longestAlternativeSequence(int a[], int n)
{
    int maxi1 = 0;
  
    // Marks the starting of odd
    // number as sequence and
    // alternatively changes
    int f1 = 0;
  
    // Finding the longest odd/even sequence
    for (int i = 0; i < n; i++) {
  
        // Find odd number
        if (!f1) {
            if (a[i] % 2) {
                f1 = 1;
                maxi1++;
            }
        }
  
        // Find even number
        else {
            if (a[i] % 2 == 0) {
                maxi1++;
                f1 = 0;
            }
        }
    }
  
    int maxi2 = 0;
    int f2 = 0;
  
    // Length of the longest even/odd sequence
    for (int i = 0; i < n; i++) {
  
        // Find odd number
        if (f2) {
            if (a[i] % 2) {
                f2 = 1;
                maxi2++;
            }
        }
  
        // Find even number
        else {
            if (a[i] % 2 == 0) {
                maxi2++;
                f2 = 0;
            }
        }
    }
  
    // Answer is maximum of both
    // odd/even or even/odd subsequence
    return max(maxi1, maxi2);
}
  
// Driver Code
int main()
{
    int a[] = { 13, 16, 8, 9, 32, 10 };
    int n = sizeof(a) / sizeof(a[0]);
    cout << longestAlternativeSequence(a, n);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the length
// of the longest alternative parity
// subsequence
class GFG 
{
  
// Function to find the longest
static int longestAlternativeSequence(int a[], int n)
{
    int maxi1 = 0;
  
    // Marks the starting of odd
    // number as sequence and
    // alternatively changes
    int f1 = 0;
  
    // Finding the longest odd/even sequence
    for (int i = 0; i < n; i++) 
    {
  
        // Find odd number
        if (f1 % 2 != 0
        {
            if (a[i] % 2 == 1
            {
                f1 = 1;
                maxi1++;
            }
        }
  
        // Find even number
        else 
        {
            if (a[i] % 2 == 0)
            {
                maxi1++;
                f1 = 0;
            }
        }
    }
  
    int maxi2 = 0;
    int f2 = 0;
  
    // Length of the longest even/odd sequence
    for (int i = 0; i < n; i++) 
    {
  
        // Find odd number
        if (f2 % 2 != 0)
        {
            if (a[i] % 2 == 1)
            {
                f2 = 1;
                maxi2++;
            }
        }
  
        // Find even number
        else 
        {
            if (a[i] % 2 == 0)
            {
                maxi2++;
                f2 = 0;
            }
        }
    }
  
    // Answer is maximum of both
    // odd/even or even/odd subsequence
    return Math.max(maxi1, maxi2);
}
  
// Driver Code
public static void main(String[] args) 
{
    int a[] = { 13, 16, 8, 9, 32, 10 };
    int n = a.length;
    System.out.println(longestAlternativeSequence(a, n));
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the length
# of the longest alternative parity
# subsequence
  
# Function to find the longest
def longestAlternativeSequence(a, n):
    maxi1 = 0
  
    # Marks the starting of odd
    # number as sequence and
    # alternatively changes
    f1 = 0
  
    # Finding the longest odd/even sequence
    for i in range(n):
  
        # Find odd number
        if (f1 == 0):
            if (a[i] % 2):
                f1 = 1
                maxi1 += 1
  
        # Find even number
        else:
            if (a[i] % 2 == 0):
                maxi1 += 1
                f1 = 0
                  
    maxi2 = 0
    f2 = 0
  
    # Length of the longest even/odd sequence
    for i in range(n):
  
        # Find odd number
        if (f2):
            if (a[i] % 2):
                f2 = 1
                maxi2 += 1
  
        # Find even number
        else:
            if (a[i] % 2 == 0):
                maxi2 += 1
                f2 = 0
  
    # Answer is maximum of both
    # odd/even or even/odd subsequence
    return max(maxi1, maxi2)
  
# Driver Code
a = [13, 16, 8, 9, 32, 10]
n = len(a)
print(longestAlternativeSequence(a, n))
  
# This code is contributed by Mohit Kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the length
// of the longest alternative parity
// subsequence
using System;
  
class GFG
{
      
// Function to find the longest
static int longestAlternativeSequence(int []a,
                                      int n)
{
    int maxi1 = 0;
  
    // Marks the starting of odd
    // number as sequence and
    // alternatively changes
    int f1 = 0;
  
    // Finding the longest odd/even sequence
    for (int i = 0; i < n; i++) 
    {
  
        // Find odd number
        if (f1 != 0) 
        {
            if (a[i] % 2 == 0) 
            {
                f1 = 1;
                maxi1++;
            }
        }
  
        // Find even number
        else
        {
            if (a[i] % 2 == 0) 
            {
                maxi1++;
                f1 = 0;
            }
        }
    }
  
    int maxi2 = 0;
    int f2 = 0;
  
    // Length of the longest even/odd sequence
    for (int i = 0; i < n; i++) 
    {
  
        // Find odd number
        if (f2 == 0) 
        {
            if (a[i] % 2 == 0)
            {
                f2 = 1;
                maxi2++;
            }
        }
  
        // Find even number
        else 
        {
            if (a[i] % 2 == 0) 
            {
                maxi2++;
                f2 = 0;
            }
        }
    }
  
    // Answer is maximum of both
    // odd/even or even/odd subsequence
    return Math.Max(maxi1, maxi2);
}
  
// Driver Code
public static void Main()
{
    int []a = { 13, 16, 8, 9, 32, 10 };
    int n = a.Length;
    Console.Write(longestAlternativeSequence(a, n));
}
}
  
// This code is contributed by Nidhi

chevron_right


Output:

4


My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

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.