Given N elements, write a program that prints the length of the longest increasing subsequence whose adjacent element difference is one.

Examples:

Input : a[] = {3, 10, 3, 11, 4, 5, 6, 7, 8, 12}

Output : 6

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 : 5

Explanation: 6, 7, 8, 9, 10 is the longest increasing subsequence

**Naive Approach: ** A normal approach will be to iterate for every element and find out the longest increasing subsequence. For any particular element, find the length of the subsequence starting from that element. Print the longest length of the subsequence thus formed. The time complexity of this approach will be O(n^{2}).

**Dynamic Programming Approach: ** Let DP[i] store the length of the longest subsequence which ends with A[i]. For every A[i], if A[i]-1 is present in the array before i-th index, then A[i] will add to the increasing subsequence which has A[i]-1. Hence **DP[i] = DP[ index(A[i]-1) ] + 1**. If A[i]-1 is not present in the array before i-th index, then DP[i]=1 since the A[i] element forms a subsequence which starts with A[i]. Hence the relation for DP[i] is:

If A[i]-1 is present before i-th index:

DP[i] = DP[ index(A[i]-1) ] + 1 else:

DP[i] = 1

Given below is the illustration 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 ` `int` `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 `
` ` `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 `
` ` `maximum = max(maximum, dp[i]); `
` ` `} `
` ` ` ` `return` `maximum; `
`} ` ` ` `// Driver Code ` `int` `main() `
`{ ` ` ` `int` `a[] = { 3, 10, 3, 11, 4, 5, 6, 7, 8, 12 }; `
` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); `
` ` `cout << longestSubsequence(a, n); `
` ` `return` `0; `
`} ` |

*chevron_right*

*filter_none*

`# python program to find length of the ` `# longest increasing subsequence ` `# whose adjacent element differ by 1 ` ` ` `from` `collections ` `import` `defaultdict `
`import` `sys `
` ` `# function that returns the length of the ` `# longest increasing subsequence ` `# whose adjacent element differ by 1 ` ` ` `def` `longestSubsequence(a,n): `
` ` `mp ` `=` `defaultdict(` `lambda` `:` `0` `) `
` ` ` ` `# stores the length of the longest `
` ` `# subsequence that ends with a[i] `
` ` `dp ` `=` `[` `0` `for` `i ` `in` `range` `(n)] `
` ` `maximum ` `=` `-` `sys.maxsize `
` ` ` ` `# iterate for all element `
` ` `for` `i ` `in` `range` `(n): `
` ` ` ` `# if a[i]-1 is present before i-th index `
` ` `if` `a[i] ` `-` `1` `in` `mp: `
` ` ` ` `# last index of a[i]-1 `
` ` `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 `
` ` `maximum ` `=` `max` `(maximum, dp[i]) `
` ` `return` `maximum `
` ` ` ` `# Driver Code ` `a ` `=` `[` `3` `, ` `10` `, ` `3` `, ` `11` `, ` `4` `, ` `5` `, ` `6` `, ` `7` `, ` `8` `, ` `12` `] `
`n ` `=` `len` `(a) `
`print` `(longestSubsequence(a, n)) `
` ` `# This code is contributed by Shrikant13 ` |

*chevron_right*

*filter_none*

**Output:**

6

**Time Complexity :** O(n)**Auxiliary Space :** O(n)

## Recommended Posts:

- Printing longest Increasing consecutive subsequence
- Longest Increasing Subsequence using Longest Common Subsequence Algorithm
- Largest increasing subsequence of consecutive integers
- Longest Increasing Subsequence using BIT
- Longest Increasing Odd Even Subsequence
- Longest Increasing Subsequence | DP-3
- Longest Common Increasing Subsequence (LCS + LIS)
- C/C++ Program for Longest Increasing Subsequence
- C++ Program for Longest Increasing Subsequence
- Construction of Longest Increasing Subsequence (N log N)
- Longest Increasing Subsequence Size (N log N)
- Python program for Longest Increasing Subsequence
- Java Program for Longest Increasing Subsequence
- Longest Consecutive Subsequence
- Construction of Longest Increasing Subsequence(LIS) and printing LIS sequence

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.