给定一个由N 个整数组成的数组arr[] ,任务是找到最长递增绝对偶数子序列的长度。
An increasing absolute even subsequence is an increasing subsequence of array elements having absolute difference between adjacent pairs as even.
例子:
Input: arr[] = {10, 22, 9, 33, 21, 50, 41, 60}
Output: 4
Explanation: The longest increasing absolute even subsequence is {10, 22, 50, 60}. Therefore, the required length is 4.
Input: arr[] = {11, -22, 43, -54, 66, 5}
Output: 3
Explanation:
The longest increasing absolute even subsequence is 3 i.e. {-22, -54, 66}. Therefore, the required length is 4.
朴素的方法:最简单的方法是生成给定数组的所有可能子序列,并对每个子序列检查子序列是否在增加,相邻元素之间的绝对差是否为偶数。打印最长的此类子序列的长度。
时间复杂度: O(2 N )
辅助空间: O(1)
高效方法:优化上述方法,其思想类似于寻找最长递增子序列。但唯一要改变的条件是检查子序列的两个相邻元素之间的绝对差是否为偶数。请按照以下步骤解决问题:
- 初始化一个辅助数组dp[] ,其中所有初始值为 1 。
- 使用变量i在[0, N)范围内遍历给定数组arr[] ,并对每个索引执行以下操作:
- 在[0, i)范围内使用变量j进行迭代并检查以下三个条件:
- 如果arr[i] 的绝对值 > arr[j] 。
- 如果arr[i]和arr[j]都是偶数与否。
- 如果dp[i] < dp[j] + 1 。
- 如果任何索引j满足上述三个条件,则更新dp[i] = dp[j] + 1 。
- 在[0, i)范围内使用变量j进行迭代并检查以下三个条件:
- 打印数组dp[]的最大元素作为所需结果。
下面是上述方法的实现:
C++14
// C++14 program for the above approach
#include
using namespace std;
// Function to find the longest
// increasing absolute even subsequence
void EvenLIS(int arr[], int n)
{
// Stores length of
// required subsequence
int lis[n];
for(int i = 0; i < n; i++)
lis[i] = 1;
// Traverse the array
for(int i = 1; i < n; i++)
{
// Traverse prefix of current
// array element
for(int j = 0; j < i; j++)
{
// Check if the subsequence is
// LIS and have even absolute
// difference of adjacent pairs
if (abs(arr[i]) > abs(arr[j]) &&
abs(arr[i]) % 2 == 0 &&
abs(arr[j]) % 2 == 0 &&
lis[i] < lis[j] + 1)
// Update lis[]
lis[i] = lis[j] + 1;
}
}
// Stores maximum length
int maxlen = 0;
// Find the length of longest
// absolute even subsequence
for(int i = 0; i < n; i++)
maxlen = max(maxlen, lis[i]);
// Return the maximum length of
// absolute even subsequence
cout << maxlen << endl;
}
// Driver code
int main()
{
// Given array arr[] and brr[]
int arr[] = { 11, -22, 43, -54, 66, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
EvenLIS(arr, N);
}
// This code is contributed by code_hunt
Java
// Java program for the above approach
import java.util.*;
import java.io.*;
class GFG{
// Function to find the longest
// increasing absolute even subsequence
static void EvenLIS(int arr[])
{
// Length of arr
int n = arr.length;
// Stores length of
// required subsequence
int lis[] = new int[n];
Arrays.fill(lis, 1);
// Traverse the array
for(int i = 1; i < n; i++)
{
// Traverse prefix of current
// array element
for(int j = 0; j < i; j++)
{
// Check if the subsequence is
// LIS and have even absolute
// difference of adjacent pairs
if (Math.abs(arr[i]) > Math.abs(arr[j]) &&
Math.abs(arr[i]) % 2 == 0 &&
Math.abs(arr[j]) % 2 == 0 &&
lis[i] < lis[j] + 1)
// Update lis[]
lis[i] = lis[j] + 1;
}
}
// Stores maximum length
int maxlen = 0;
// Find the length of longest
// absolute even subsequence
for(int i = 0; i < n; i++)
maxlen = Math.max(maxlen, lis[i]);
// Return the maximum length of
// absolute even subsequence
System.out.println(maxlen);
}
// Driver code
public static void main(String args[])
{
// Given array arr[] and brr[]
int arr[] = { 11, -22, 43, -54, 66, 5 };
int N = arr.length;
// Function call
EvenLIS(arr);
}
}
// This code is contributed by bikram2001jha
Python3
# Python3 program for the above approach
# Function to find the longest
# increasing absolute even subsequence
def EvenLIS(arr):
# Length of arr
n = len(arr)
# Stores length of
# required subsequence
lis = [1]*n
# Traverse the array
for i in range(1, n):
# Traverse prefix of current
# array element
for j in range(0, i):
# Check if the subsequence is
# LIS and have even absolute
# difference of adjacent pairs
if abs(arr[i]) > abs(arr[j]) \
and abs(arr[i] % 2) == 0 \
and abs(arr[j] % 2) == 0 \
and lis[i] < lis[j]+1:
# Update lis[]
lis[i] = lis[j]+1
# Stores maximum length
maxlen = 0
# Find the length of longest
# absolute even subsequence
for i in range(n):
maxlen = max(maxlen, lis[i])
# Return the maximum length of
# absolute even subsequence
print(maxlen)
# Driver Code
# Given arr[]
arr = [11, -22, 43, -54, 66, 5]
# Function Call
EvenLIS(arr)
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the longest
// increasing absolute even subsequence
static void EvenLIS(int []arr)
{
// Length of arr
int n = arr.Length;
// Stores length of
// required subsequence
int []lis = new int[n];
for(int i = 0; i < n; i++)
lis[i] = 1;
// Traverse the array
for(int i = 1; i < n; i++)
{
// Traverse prefix of current
// array element
for(int j = 0; j < i; j++)
{
// Check if the subsequence is
// LIS and have even absolute
// difference of adjacent pairs
if (Math.Abs(arr[i]) > Math.Abs(arr[j]) &&
Math.Abs(arr[i]) % 2 == 0 &&
Math.Abs(arr[j]) % 2 == 0 &&
lis[i] < lis[j] + 1)
// Update lis[]
lis[i] = lis[j] + 1;
}
}
// Stores maximum length
int maxlen = 0;
// Find the length of longest
// absolute even subsequence
for(int i = 0; i < n; i++)
maxlen = Math.Max(maxlen, lis[i]);
// Return the maximum length of
// absolute even subsequence
Console.WriteLine(maxlen);
}
// Driver code
public static void Main(String []args)
{
// Given array []arr and brr[]
int []arr = { 11, -22, 43, -54, 66, 5 };
int N = arr.Length;
// Function call
EvenLIS(arr);
}
}
// This code is contributed by Amit Katiyar
Javascript
3
时间复杂度: O(N 2 )
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。