Given n elements, write a program that prints the longest increasing subsequence whose adjacent element difference is one.
Examples:
Input : a[] = {3, 10, 3, 11, 4, 5, 6, 7, 8, 12}
Output : 3 4 5 6 7 8
Explanation: 3, 4, 5, 6, 7, 8 is the longest increasing subsequence whose adjacent element differs by one.
Input : a[] = {6, 7, 8, 3, 4, 5, 9, 10}
Output : 6 7 8 9 10
Explanation: 6, 7, 8, 9, 10 is the longest increasing subsequence
We have discussed how to find length of Longest Increasing consecutive subsequence. To print the subsequence, we store index of last element. Then we print consecutive elements ending with last element.
Given below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void longestSubsequence( int a[], int n)
{
unordered_map< int , int > mp;
int dp[n];
memset (dp, 0, sizeof (dp));
int maximum = INT_MIN;
int index = -1;
for ( int i = 0; i < n; i++) {
if (mp.find(a[i] - 1) != mp.end()) {
int lastIndex = mp[a[i] - 1] - 1;
dp[i] = 1 + dp[lastIndex];
}
else
dp[i] = 1;
mp[a[i]] = i + 1;
if (maximum < dp[i]) {
maximum = dp[i];
index = i;
}
}
for ( int curr = a[index] - maximum + 1;
curr <= a[index]; curr++)
cout << curr << " " ;
}
int main()
{
int a[] = { 3, 10, 3, 11, 4, 5, 6, 7, 8, 12 };
int n = sizeof (a) / sizeof (a[0]);
longestSubsequence(a, n);
return 0;
}
|
Java
import java.util.HashMap;
class GFG
{
public static void longestSubsequence( int [] a,
int n)
{
HashMap<Integer,
Integer> mp = new HashMap<>();
int [] dp = new int [n];
int maximum = Integer.MIN_VALUE;
int index = - 1 ;
for ( int i = 0 ; i < n; i++)
{
if (mp.get(a[i] - 1 ) != null )
{
int lastIndex = mp.get(a[i] - 1 ) - 1 ;
dp[i] = 1 + dp[lastIndex];
}
else
dp[i] = 1 ;
mp.put(a[i], i + 1 );
if (maximum < dp[i])
{
maximum = dp[i];
index = i;
}
}
for ( int curr = a[index] - maximum + 1 ;
curr <= a[index]; curr++)
System.out.print(curr + " " );
}
public static void main(String[] args)
{
int [] a = { 3 , 10 , 3 , 11 , 4 ,
5 , 6 , 7 , 8 , 12 };
int n = a.length;
longestSubsequence(a, n);
}
}
|
Python3
import sys
def longestSubsequence(a, n):
mp = {i: 0 for i in range ( 13 )}
dp = [ 0 for i in range (n)]
maximum = - sys.maxsize - 1
index = - 1
for i in range (n):
if ((a[i] - 1 ) in mp):
lastIndex = mp[a[i] - 1 ] - 1
dp[i] = 1 + dp[lastIndex]
else :
dp[i] = 1
mp[a[i]] = i + 1
if (maximum < dp[i]):
maximum = dp[i]
index = i
for curr in range (a[index] - maximum + 1 ,
a[index] + 1 , 1 ):
print (curr, end = " " )
if __name__ = = '__main__' :
a = [ 3 , 10 , 3 , 11 , 4 , 5 ,
6 , 7 , 8 , 12 ]
n = len (a)
longestSubsequence(a, n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void longestSubsequence( int [] a, int n)
{
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
int [] dp = new int [n];
int maximum = -100000000;
int index = -1;
for ( int i = 0; i < n; i++)
{
if (mp.ContainsKey(a[i] - 1) == true )
{
int lastIndex = mp[a[i] - 1] - 1;
dp[i] = 1 + dp[lastIndex];
}
else
dp[i] = 1;
mp[a[i]] = i + 1;
if (maximum < dp[i])
{
maximum = dp[i];
index = i;
}
}
for ( int curr = a[index] - maximum + 1;
curr <= a[index]; curr++)
Console.Write(curr + " " );
}
static void Main()
{
int [] a = { 3, 10, 3, 11, 4,
5, 6, 7, 8, 12 };
int n = a.Length;
longestSubsequence(a, n);
}
}
|
Javascript
<script>
function longestSubsequence(a, n)
{
let mp = new Map();
let dp = new Array(n);
let maximum = Number.MIN_VALUE;
let index = -1;
for (let i = 0; i < n; i++)
{
if (mp.get(a[i] - 1) != null )
{
let lastIndex = mp.get(a[i] - 1) - 1;
dp[i] = 1 + dp[lastIndex];
}
else
dp[i] = 1;
mp.set(a[i], i + 1);
if (maximum < dp[i])
{
maximum = dp[i];
index = i;
}
}
for (let curr = a[index] - maximum + 1;
curr <= a[index]; curr++)
document.write(curr + " " );
}
let a=[3, 10, 3, 11, 4,
5, 6, 7, 8, 12 ];
let n = a.length;
longestSubsequence(a, n);
</script>
|
Complexity Analysis:
- Time Complexity: O(n), as we are using a loop to traverse n times and in each traversal.
- Auxiliary Space: O(n), as we are using extra space for dp and mp.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
17 Aug, 2022
Like Article
Save Article