Maximum length subsequence with difference between adjacent elements as either 0 or 1 | Set 2
Last Updated :
19 Jul, 2022
Given an array of n integers. The problem is to find maximum length of the subsequence with difference between adjacent elements in the subsequence as either 0 or 1. Time Complexity of O(n) is required.
Examples:
Input : arr[] = {2, 5, 6, 3, 7, 6, 5, 8}
Output : 5
The subsequence is {5, 6, 7, 6, 5}.
Input : arr[] = {-2, -1, 5, -1, 4, 0, 3}
Output : 4
The subsequence is {-2, -1, -1, 0}.
Method 1: Previously an approach having time complexity of O(n2) have been discussed in this post.
Method 2 (Efficient Approach):
The idea is to create a hash map having tuples in the form (ele, len), where len denotes the length of the longest subsequence ending with the element ele. Now, for each element arr[i] we can find the length of the values arr[i]-1, arr[i] and arr[i]+1 in the hash table and consider the maximum among them. Let this maximum value be max. Now, the length of longest subsequence ending with arr[i] would be max+1. Update this length along with the element arr[i] in the hash table. Finally, the element having the maximum length in the hash table gives the maximum length subsequence.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int maxLenSub( int arr[], int n)
{
unordered_map< int , int > um;
int maxLen = 0;
for ( int i=0; i<n; i++)
{
int len = 0;
if (um.find(arr[i]-1) != um.end() && len < um[arr[i]-1])
len = um[arr[i]-1];
if (um.find(arr[i]) != um.end() && len < um[arr[i]])
len = um[arr[i]];
if (um.find(arr[i]+1) != um.end() && len < um[arr[i]+1])
len = um[arr[i]+1];
um[arr[i]] = len + 1;
if (maxLen < um[arr[i]])
maxLen = um[arr[i]];
}
return maxLen;
}
int main()
{
int arr[] = {2, 5, 6, 3, 7, 6, 5, 8};
int n = sizeof (arr) / sizeof (arr[0]);
cout << "Maximum length subsequence = "
<< maxLenSub(arr, n);
return 0;
}
|
Java
import java.util.HashMap;
class GFG
{
public static int maxLengthSub( int [] arr)
{
int max_val = 0 ;
int start = 0 ;
HashMap<Integer, Integer> map = new HashMap<>();
for ( int i = 0 ; i < arr.length; i++)
{
int temp = 0 ;
if (map.containsKey(arr[i] - 1 ))
{
temp = map.get(arr[i] - 1 );
}
if (map.containsKey(arr[i]))
{
temp = Math.max(temp, map.get(arr[i]));
}
if (map.containsKey(arr[i] + 1 ))
{
temp = Math.max(temp, map.get(arr[i] + 1 ));
}
temp++;
if (temp > max_val)
{
max_val = temp;
}
map.put(arr[i], temp);
}
return max_val;
}
public static void main(String[] args)
{
int arr[] = { 2 , 5 , 6 , 3 , 7 , 6 , 5 , 8 };
System.out.println(maxLengthSub(arr));
}
}
|
Python3
from collections import defaultdict
def maxLenSub(arr, n):
um = defaultdict( lambda : 0 )
maxLen = 0
for i in range ( 0 , n):
length = 0
if (arr[i] - 1 ) in um and length < um[arr[i] - 1 ]:
length = um[arr[i] - 1 ]
if arr[i] in um and length < um[arr[i]]:
length = um[arr[i]]
if (arr[i] + 1 ) in um and length < um[arr[i] + 1 ]:
length = um[arr[i] + 1 ]
um[arr[i]] = length + 1
if maxLen < um[arr[i]]:
maxLen = um[arr[i]]
return maxLen
if __name__ = = "__main__" :
arr = [ 2 , 5 , 6 , 3 , 7 , 6 , 5 , 8 ]
n = len (arr)
print ( "Maximum length subsequence =" , maxLenSub(arr, n))
|
C#
Javascript
<script>
function maxLenSub(arr, n)
{
var um = new Map();
var maxLen = 0;
for ( var i=0; i<n; i++)
{
var len = 0;
if (um.has(arr[i]-1) && len < um.get(arr[i]-1))
len = um.get(arr[i]-1);
if (um.has(arr[i]) && len < um.get(arr[i]))
len = um.get(arr[i]);
if (um.has(arr[i]+1) && len < um.get(arr[i]+1))
len = um.get(arr[i]+1);
um.set(arr[i], len + 1);
if (maxLen < um.get(arr[i]))
maxLen = um.get(arr[i]);
}
return maxLen;
}
var arr = [2, 5, 6, 3, 7, 6, 5, 8];
var n = arr.length;
document.write( "Maximum length subsequence = "
+ maxLenSub(arr, n));
</script>
|
Output
Maximum length subsequence = 5
Time Complexity: O(n)
Auxiliary Space: O(n)
Thanks to Neeraj for suggesting the above solution in the comments of this post.
Share your thoughts in the comments
Please Login to comment...