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 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:
- Initialize an auxiliary array dp[] where all are initially 1.
- 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:
- If absolute value of arr[i] > arr[j].
- If arr[i] and arr[j] both are even or not.
- If dp[i] < dp[j] + 1.
- If the above three conditions are satisfied for any index j, then update dp[i] = dp[j] + 1.
- Print the maximum element of the array dp[] as the required result.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
void EvenLIS( int arr[], int n)
{
int lis[n];
for ( int i = 0; i < n; i++)
lis[i] = 1;
for ( int i = 1; i < n; i++)
{
for ( int j = 0; j < i; j++)
{
if ( abs (arr[i]) > abs (arr[j]) &&
abs (arr[i]) % 2 == 0 &&
abs (arr[j]) % 2 == 0 &&
lis[i] < lis[j] + 1)
lis[i] = lis[j] + 1;
}
}
int maxlen = 0;
for ( int i = 0; i < n; i++)
maxlen = max(maxlen, lis[i]);
cout << maxlen << endl;
}
int main()
{
int arr[] = { 11, -22, 43, -54, 66, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
EvenLIS(arr, N);
}
|
Java
import java.util.*;
import java.io.*;
class GFG{
static void EvenLIS( int arr[])
{
int n = arr.length;
int lis[] = new int [n];
Arrays.fill(lis, 1 );
for ( int i = 1 ; i < n; i++)
{
for ( int j = 0 ; j < i; j++)
{
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 )
lis[i] = lis[j] + 1 ;
}
}
int maxlen = 0 ;
for ( int i = 0 ; i < n; i++)
maxlen = Math.max(maxlen, lis[i]);
System.out.println(maxlen);
}
public static void main(String args[])
{
int arr[] = { 11 , - 22 , 43 , - 54 , 66 , 5 };
int N = arr.length;
EvenLIS(arr);
}
}
|
Python3
def EvenLIS(arr):
n = len (arr)
lis = [ 1 ] * n
for i in range ( 1 , n):
for j in range ( 0 , i):
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 :
lis[i] = lis[j] + 1
maxlen = 0
for i in range (n):
maxlen = max (maxlen, lis[i])
print (maxlen)
arr = [ 11 , - 22 , 43 , - 54 , 66 , 5 ]
EvenLIS(arr)
|
C#
using System;
class GFG{
static void EvenLIS( int []arr)
{
int n = arr.Length;
int []lis = new int [n];
for ( int i = 0; i < n; i++)
lis[i] = 1;
for ( int i = 1; i < n; i++)
{
for ( int j = 0; j < i; j++)
{
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)
lis[i] = lis[j] + 1;
}
}
int maxlen = 0;
for ( int i = 0; i < n; i++)
maxlen = Math.Max(maxlen, lis[i]);
Console.WriteLine(maxlen);
}
public static void Main(String []args)
{
int []arr = { 11, -22, 43, -54, 66, 5 };
int N = arr.Length;
EvenLIS(arr);
}
}
|
Javascript
<script>
function EvenLIS(arr)
{
let n = arr.length;
let lis = new Array(n).fill(1);
for (let i = 1; i < n; i++)
{
for (let j = 0; j < i; j++)
{
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)
lis[i] = lis[j] + 1;
}
}
let maxlen = 0;
for (let i = 0; i < n; i++)
maxlen = Math.max(maxlen, lis[i]);
document.write(maxlen);
}
let arrr = [ 11, -22, 43, -54, 66, 5 ];
let N = arrr.length;
EvenLIS(arrr);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Last Updated :
26 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...