📌  相关文章
📜  最长增加的绝对偶数子序列的长度

📅  最后修改于: 2021-05-17 02:17:44             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是找到最长的增加的绝对偶数子序列的长度。

例子:

天真的方法:最简单的方法是生成给定数组的所有可能的子序列,并针对每个子序列检查子序列是否在增加,并且相邻元素之间的绝对差是否相等。打印最长的此类子序列的长度。

时间复杂度: O(2 N )
辅助空间: O(1)

高效的方法:为了优化上述方法,该想法类似于找到最长的递增子序列。但是唯一要更改的条件是检查子序列的两个相邻元素之间的绝对差是否相等。请按照以下步骤解决问题:

  1. 初始化一个辅助数组dp [] ,其中所有初始都为1
  2. 使用变量i[0,N)范围内遍历给定数组arr [] ,并对每个索引执行以下操作:
    • [0,i)范围内使用变量j进行迭代并检查以下三个条件:
      1. 如果arr [i]的绝对值> arr [j]
      2. 如果arr [i]arr [j]都为偶数或不为偶数。
      3. 如果dp [i] < dp [j] + 1
    • 如果对于任何索引j都满足以上三个条件,则更新dp [i] = dp [j] + 1
  3. 将数组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
    // abolute 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
    // abolute 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
    # abolute 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
    // abolute 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


输出:
3





时间复杂度: O(N 2 )
辅助空间: O(N)