Length of longest increasing absolute even subsequence

Given an array arr[] consisting of N integers, the task is to find the length of the longest increasing absolute even subsequence.

An increasing absolute even subsequence is an increasing subsequence of array elements having absolute difference between adjacent pairs as even.

Examples:

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.



Naive Approach: The simplest approach is to generate all possible subsequence of the given array and for each subsequence, check if the subsequence is increasing and absolute difference between adjacent elements is even or not. Print the length of the longest such subsequence. 

Time Complexity: O(2N)
Auxiliary Space: O(1)

Efficient Approach: To optimize the above approach, the idea is similar to finding longest increasing subsequence. But the only condition to be changed is to check if the absolute difference between two adjacent elements of the subsequence is even or not. Follow the steps below to solve the problem:

  1. Initialize an auxiliary array dp[] where all are initially 1.
  2. Traverse the given array arr[] using variable i over the range [0, N) and for each index do the following:
    • Iterate using variable j over the range [0, i) and check for the following three conditions:
      1. If absolute value of arr[i] > arr[j].
      2. If arr[i] and arr[j] both are even or not.
      3. If dp[i] < dp[j] + 1.
    • If the above three conditions are satisfied for any index j, then update dp[i] = dp[j] + 1.
  3. Print the maximum element of the array dp[] as the required result.

Below is the implementation of the above approach:

C++14

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++14 program for the above approach
#include <bits/stdc++.h>
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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output: 

3





Time Complexity: O(N2)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.