Longest Increasing consecutive subsequence | Set-2
Given an array arr[] of N elements, the task is to find the length of the longest increasing subsequence whose adjacent element difference is one.
Examples:
Input: arr[] = {3, 10, 3, 11, 4, 5, 6, 7, 8, 12}
Output: 6
Explanation: The subsequence {3, 4, 5, 6, 7, 8} is the longest increasing subsequence whose adjacent elements differs by one.
Input: arr[] = {6, 7, 8, 3, 4, 5, 9, 10}
Output: 5
Approach: The naive and the dynamic programming approach has already been discussed in this article. This article discusses a simpler and easy to implement approach using Hashing. The idea is to store the length of subsequence ending with the current integer X in an unordered map m. Hence, if X + 1 occurs during the traversal, the length of the subsequence will be m[X] + 1. The maximum value in the map m is the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int longestSubsequence( int arr[], int N)
{
unordered_map< int , int > m;
int ans = 0;
for ( int i = 0; i < N; i++) {
m[arr[i]] = max(m[arr[i]],
m[arr[i] - 1] + 1);
ans = max(ans, m[arr[i]]);
}
return ans;
}
int main()
{
int arr[] = { 3, 4, 2, 6, 5, 3, 7, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << longestSubsequence(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static int longestSubsequence( int [] arr, int N)
{
HashMap<Integer, Integer> m = new HashMap<>();
int ans = 0 ;
for ( int i = 0 ; i < N; i++) {
if (m.containsKey(arr[i])) {
m.put(arr[i],
Math.max(m.get(arr[i]),
m.get(arr[i] - 1 ) + 1 ));
}
else {
m.put(arr[i], 1 );
}
ans = Math.max(ans, m.get(arr[i]));
}
return ans;
}
public static void main(String[] args)
{
int [] arr = new int [] { 3 , 4 , 2 , 6 , 5 , 3 , 7 , 4 , 5 };
int N = arr.length;
System.out.print(longestSubsequence(arr, N));
}
}
|
Python
def longestSubsequence(arr, N):
m = {}
ans = 0
for i in range ( 0 , N):
if arr[i] in m and arr[i] - 1 in m:
m[arr[i]] = max (m[arr[i]], m[arr[i] - 1 ] + 1 )
else :
m[arr[i]] = 1
ans = max (ans, m[arr[i]])
return ans
arr = [ 3 , 4 , 2 , 6 , 5 , 3 , 7 , 4 , 5 ]
N = len (arr)
print (longestSubsequence(arr, N))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
public static int longestSubsequence( int [] arr, int N)
{
Dictionary< int , int > m = new Dictionary< int , int >();
int ans = 0;
for ( int i = 0; i < N; i++) {
if (m.ContainsKey(arr[i])) {
m[arr[i]]=
Math.Max(m[arr[i]],
m[arr[i] - 1] + 1);
}
else {
m.Add(arr[i], 1);
}
ans = Math.Max(ans, m[arr[i]]);
}
return ans;
}
public static void Main(String[] args)
{
int [] arr = new int [] { 3, 4, 2, 6, 5, 3, 7, 4, 5 };
int N = arr.Length;
Console.Write(longestSubsequence(arr, N));
}
}
|
Javascript
<script>
function longestSubsequence(arr, N)
{
let m = new Map();
let ans = 0;
for (let i = 0; i < N; i++)
{
if (m.has(arr[i]))
m.set(arr[i], Math.max(m.get(arr[i]),
m.get(arr[i] - 1) + 1));
else
m.set(arr[i], 1)
ans = Math.max(ans, m.get(arr[i]));
}
return ans;
}
let arr = [3, 4, 2, 6, 5, 3, 7, 4, 5];
let N = arr.length;
document.write(longestSubsequence(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
24 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...