# Longest subsequence such that absolute difference between every pair is atmost 1

Given an integer array **arr[]** of size **N**, the task is to find the longest subsequence **S** such that for every a[i], a[j] ∈ S and **|a[i] – a[j]| ≤ 1**.

**Examples:**

Input:arr[] = {2, 2, 3, 5, 5, 6, 6, 6}

Output:5

Explanation:

There are 2 such subsequence such that difference between every pair is atmost 1

{2, 2, 3} and {5, 5, 6, 6, 6}

The longest one of these is {5, 5, 6, 6, 6} with length of 5.

Input:arr[] = {5, 7, 6, 4, 4, 2}

Output:3

**Approach:**

The idea is to observe that for a subsequence with difference between every possible pair atmost one is possible when the subsequence contains elements between [X , X + 1].

- Intialize the maximum length of required subsequence to 0.
- Create a HashMap to store frequency of every element of the array.
- Interate through the Hash Map and for every element a[i] in hash map –
- Find the count of occurence of element (a[i] + 1), (a[i]) and (a[i] – 1).
- Find the Maximum count out of occurence of elements (a[i] + 1) or (a[i] – 1).
- If the Total count of occurence is greater than the maximum length found then update the maximum length of subsequence.

Below is the implementation of above approach.

## Java

`// Java implementation for ` `// Longest subsequence such that absolute ` `// difference between every pair is atmost 1 ` ` ` `import` `java.util.*; ` `public` `class` `GeeksForGeeks { ` ` ` `public` `static` `int` `longestAr( ` ` ` `int` `n, ` `int` `arr[]){ ` ` ` `Hashtable<Integer, Integer> count ` ` ` `= ` `new` `Hashtable<Integer, Integer>(); ` ` ` ` ` `// Storing the frequency of each ` ` ` `// element in the hashtable count ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` `if` `(count.containsKey(arr[i])) ` ` ` `count.put(arr[i], count.get( ` ` ` `arr[i]) + ` `1` ` ` `); ` ` ` `else` ` ` `count.put(arr[i], ` `1` `); ` ` ` `} ` ` ` ` ` `Set<Integer> kset = count.keySet(); ` ` ` `Iterator<Integer> it = kset.iterator(); ` ` ` ` ` `// Max is used to keep a track of ` ` ` `// maximum length of the required ` ` ` `// subsequence so far. ` ` ` `int` `max = ` `0` `; ` ` ` ` ` `while` `(it.hasNext()) { ` ` ` `int` `a = (` `int` `)it.next(); ` ` ` `int` `cur = ` `0` `; ` ` ` `int` `cur1 = ` `0` `; ` ` ` `int` `cur2 = ` `0` `; ` ` ` ` ` `// Store frequency of the ` ` ` `// given element+1. ` ` ` `if` `(count.containsKey(a + ` `1` `)) ` ` ` `cur1 = count.get(a + ` `1` `); ` ` ` ` ` `// Store frequency of the ` ` ` `// given element-1. ` ` ` `if` `(count.containsKey(a - ` `1` `)) ` ` ` `cur2 = count.get(a - ` `1` `); ` ` ` ` ` `// cur store the longest array ` ` ` `// that can be formed using a. ` ` ` `cur = count.get(a) + ` ` ` `Math.max(cur1, cur2); ` ` ` ` ` `// update max if cur>max. ` ` ` `if` `(cur > max) ` ` ` `max = cur; ` ` ` `} ` ` ` ` ` `return` `(max); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `8` `; ` ` ` `int` `arr[] = { ` `2` `, ` `2` `, ` `3` `, ` `5` `, ` `5` `, ` `6` `, ` `6` `, ` `6` `}; ` ` ` `int` `maxLen = longestAr(n, arr); ` ` ` `System.out.println(maxLen); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation for ` `# Longest subsequence such that absolute ` `# difference between every pair is atmost 1 ` ` ` `def` `longestAr(n, arr): ` ` ` `count ` `=` `dict` `() ` ` ` ` ` `# Storing the frequency of each ` ` ` `# element in the hashtable count ` ` ` `for` `i ` `in` `arr: ` ` ` `count[i] ` `=` `count.get(i, ` `0` `) ` `+` `1` ` ` ` ` `kset ` `=` `count.keys() ` ` ` ` ` `# Max is used to keep a track of ` ` ` `# maximum length of the required ` ` ` `# subsequence so far. ` ` ` `maxm ` `=` `0` ` ` ` ` `for` `it ` `in` `list` `(kset): ` ` ` `a ` `=` `it ` ` ` `cur ` `=` `0` ` ` `cur1 ` `=` `0` ` ` `cur2 ` `=` `0` ` ` ` ` `# Store frequency of the ` ` ` `# given element+1. ` ` ` `if` `((a ` `+` `1` `) ` `in` `count): ` ` ` `cur1 ` `=` `count[a ` `+` `1` `] ` ` ` ` ` `# Store frequency of the ` ` ` `# given element-1. ` ` ` `if` `((a ` `-` `1` `) ` `in` `count): ` ` ` `cur2 ` `=` `count[a ` `-` `1` `] ` ` ` ` ` `# cur store the longest array ` ` ` `# that can be formed using a. ` ` ` `cur ` `=` `count[a] ` `+` `max` `(cur1, cur2) ` ` ` ` ` `# update maxm if cur>maxm. ` ` ` `if` `(cur > maxm): ` ` ` `maxm ` `=` `cur ` ` ` ` ` `return` `maxm ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `8` ` ` `arr ` `=` `[` `2` `, ` `2` `, ` `3` `, ` `5` `, ` `5` `, ` `6` `, ` `6` `, ` `6` `] ` ` ` `maxLen ` `=` `longestAr(n, arr) ` ` ` `print` `(maxLen) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

**Output:**

5

**Time Complexity:** O(n).

**Space Complexity:** O(n).

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

## Recommended Posts:

- Longest subsequence having difference atmost K
- Pair with minimum absolute difference | BST
- Check if a pair with given absolute difference exists in a Matrix
- Pair of fibonacci numbers with a given sum and minimum absolute difference
- Pair with minimum absolute difference after solving each query
- Longest subarray in which absolute difference between any two element is not greater than X
- Longest subarray with absolute difference between elements less than or equal to K using Heaps
- Longest subsequence such that difference between adjacents is one | Set 2
- Longest Increasing Subsequence using Longest Common Subsequence Algorithm
- Longest Subarray having sum of elements atmost 'k'
- Length of Longest Subarray with same elements in atmost K increments
- Find the length of the longest subarray with atmost K occurrences of the integer X
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Given an absolute sorted array and a number K, find the pair whose sum is K
- Min and max length subarray having adjacent element difference atmost K

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.