# Length of longest increasing absolute even subsequence

• Last Updated : 26 Jul, 2021

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

 `// 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);``     ` `    ``// 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

 ``
Output:
`3`

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

My Personal Notes arrow_drop_up