Longest subsequence such that difference between adjacents is one | Set 2
Last Updated :
19 May, 2021
Given an array of size n. The task is to find the longest subsequence such that difference between adjacents is one. Time Complexity of O(n) is required.
Examples:
Input : arr[] = {10, 9, 4, 5, 4, 8, 6}
Output : 3
As longest subsequences with difference 1 are, "10, 9, 8",
"4, 5, 4" and "4, 5, 6".
Input : arr[] = {1, 2, 3, 2, 3, 7, 2, 1}
Output : 7
As longest consecutive sequence is "1, 2, 3, 2, 3, 2, 1".
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 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 longest length subsequence.
C++
#include <bits/stdc++.h>
using namespace std;
int longLenSub( int arr[], int n)
{
unordered_map< int , int > um;
int longLen = 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]+1) != um.end() &&
len < um[arr[i]+1])
len = um[arr[i]+1];
um[arr[i]] = len + 1;
if (longLen < um[arr[i]])
longLen = um[arr[i]];
}
return longLen;
}
int main()
{
int arr[] = {1, 2, 3, 4, 5, 3, 2};
int n = sizeof (arr) / sizeof (arr[0]);
cout << "Longest length subsequence = "
<< longLenSub(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int longLenSub( int []arr, int n)
{
HashMap<Integer,
Integer> um = new HashMap<Integer,
Integer>();
int longLen = 0 ;
for ( int i = 0 ; i < n; i++)
{
int len = 0 ;
if (um.containsKey(arr[i] - 1 ) &&
len < um.get(arr[i] - 1 ))
len = um.get(arr[i] - 1 );
if (um.containsKey(arr[i] + 1 ) &&
len < um.get(arr[i] + 1 ))
len = um.get(arr[i] + 1 );
um. put(arr[i], len + 1 );
if (longLen < um.get(arr[i]))
longLen = um.get(arr[i]);
}
return longLen;
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 , 5 , 3 , 2 };
int n = arr.length;
System.out.println( "Longest length subsequence = " +
longLenSub(arr, n));
}
}
|
Python3
from collections import defaultdict
def longLenSub(arr, n):
um = defaultdict( lambda : 0 )
longLen = 0
for i in range (n):
len1 = 0
if (arr[i - 1 ] in um and
len1 < um[arr[i] - 1 ]):
len1 = um[arr[i] - 1 ]
if (arr[i] + 1 in um and
len1 < um[arr[i] + 1 ]):
len1 = um[arr[i] + 1 ]
um[arr[i]] = len1 + 1
if longLen < um[arr[i]]:
longLen = um[arr[i]]
return longLen
arr = [ 1 , 2 , 3 , 4 , 5 , 3 , 2 ]
n = len (arr)
print ( "Longest length subsequence =" ,
longLenSub(arr, n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int longLenSub( int []arr, int n)
{
Dictionary< int ,
int > um = new Dictionary< int ,
int >();
int longLen = 0;
for ( int i = 0; i < n; i++)
{
int len = 0;
if (um.ContainsKey(arr[i] - 1) &&
len < um[arr[i] - 1])
len = um[arr[i] - 1];
if (um.ContainsKey(arr[i] + 1) &&
len < um[arr[i] + 1])
len = um[arr[i] + 1];
um[arr[i]] = len + 1;
if (longLen < um[arr[i]])
longLen = um[arr[i]];
}
return longLen;
}
static void Main()
{
int [] arr = {1, 2, 3, 4, 5, 3, 2};
int n = arr.Length;
Console.Write( "Longest length subsequence = " +
longLenSub(arr, n));
}
}
|
Javascript
<script>
function longLenSub(arr,n)
{
let um = new Map();
let longLen = 0;
for (let i = 0; i < n; i++)
{
let 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] + 1) &&
len < um.get(arr[i] + 1))
len = um.get(arr[i] + 1);
um.set(arr[i], len + 1);
if (longLen < um.get(arr[i]))
longLen = um.get(arr[i]);
}
return longLen;
}
let arr=[1, 2, 3, 4, 5, 3, 2];
let n = arr.length;
document.write( "Longest length subsequence = " +
longLenSub(arr, n));
</script>
|
Output:
Longest length subsequence = 6
Time Complexity: O(n).
Auxiliary Space: O(n).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...