给定大小为N的数组a 。任务是打印最长的替代奇/偶或偶/奇子序列的长度。
例子:
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
方法:最长替代奇偶校验子序列的答案将是[奇,偶,奇,偶,…..]或[偶,奇,偶,奇,…。]序列。因此,在数组中进行迭代,首先找到最长的奇/偶子序列,然后是最长的偶/奇序列。找到最长子序列的步骤是:
- 迭代并找到下一个奇数并增加长度。
- 迭代并找到下一个奇数并增加长度。
- 重复步骤1和步骤2,或者从步骤1开始直到结束,以找到最长的奇/偶子序列。
- 重复步骤1和步骤2,或者从步骤2开始直到结束,以找到最长的偶数/奇数子序列。
下面是上述方法的实现:
C++
// C++ program to find the length
// of the longest alternative parity
// subsequence
#include
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);
}
Java
// 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
Python3
# 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
C#
// 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
输出:
4