# Printing longest Increasing consecutive subsequence

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:

`// CPP program to find length of the ` `// longest increasing subsequence ` `// whose adjacent element differ by 1 ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function that returns the length of the ` `// longest increasing subsequence ` `// whose adjacent element differ by 1 ` `void` `longestSubsequence(` `int` `a[], ` `int` `n) ` `{ ` ` ` `// stores the index of elements ` ` ` `unordered_map<` `int` `, ` `int` `> mp; ` ` ` ` ` `// stores the length of the longest ` ` ` `// subsequence that ends with a[i] ` ` ` `int` `dp[n]; ` ` ` `memset` `(dp, 0, ` `sizeof` `(dp)); ` ` ` ` ` `int` `maximum = INT_MIN; ` ` ` ` ` `// iterate for all element ` ` ` `int` `index = -1; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// if a[i]-1 is present before i-th index ` ` ` `if` `(mp.find(a[i] - 1) != mp.end()) { ` ` ` ` ` `// last index of a[i]-1 ` ` ` `int` `lastIndex = mp[a[i] - 1] - 1; ` ` ` ` ` `// relation ` ` ` `dp[i] = 1 + dp[lastIndex]; ` ` ` `} ` ` ` `else` ` ` `dp[i] = 1; ` ` ` ` ` `// stores the index as 1-index as we need to ` ` ` `// check for occurrence, hence 0-th index ` ` ` `// will not be possible to check ` ` ` `mp[a[i]] = i + 1; ` ` ` ` ` `// stores the longest length ` ` ` `if` `(maximum < dp[i]) { ` ` ` `maximum = dp[i]; ` ` ` `index = i; ` ` ` `} ` ` ` `} ` ` ` ` ` `// We know last element of sequence is ` ` ` `// a[index]. We also know that length ` ` ` `// of subsequence is "maximum". So We ` ` ` `// print these many consecutive elements ` ` ` `// starting from "a[index] - maximum + 1" ` ` ` `// to a[index]. ` ` ` `for` `(` `int` `curr = a[index] - maximum + 1; ` ` ` `curr <= a[index]; curr++) ` ` ` `cout << curr << ` `" "` `; ` `} ` ` ` `// Driver Code ` `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; ` `} ` |

*chevron_right*

*filter_none*

Output:

3 4 5 6 7 8

Time Complexity : O(n)

Auxiliary Space : O(n)

## Recommended Posts:

- Longest Increasing consecutive subsequence
- Printing Longest Common Subsequence | Set 2 (Printing All)
- Printing Maximum Sum Increasing Subsequence
- Printing Longest Bitonic Subsequence
- Largest increasing subsequence of consecutive integers
- Longest Increasing Odd Even Subsequence
- Longest Increasing Subsequence | DP-3
- C/C++ Program for Longest Increasing Subsequence
- C++ Program for Longest Increasing Subsequence
- Longest Common Increasing Subsequence (LCS + LIS)
- Longest Increasing Subsequence Size (N log N)
- Construction of Longest Increasing Subsequence (N log N)
- Python program for Longest Increasing Subsequence
- Java Program for Longest Increasing Subsequence
- Longest Consecutive Subsequence

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.