# Longest Consecutive Subsequence in Java

An integer array is given to us. Our task is to find the length of the longest consecutive sub-sequence of integers in the input array. In the input array, the consecutive integer may or may not be occurring together.

Example: 1

Input:

int arr[] = {11, 39, 13, 10, 14, 20, 12, 15}

Output: 6

Explanation: The sub-sequence consisting of the elements 11, 13, 10, 14, 12, and 15 is the longest sub-sequence of consecutive integers that is of the length 6. Hence, the output is 6.

Example: 2

Input:

int arr[] = {136, 141, 156, 135, 144, 133, 134, 192, 143, 132, 142}

Output: 6

Explanation: The sub-sequence consisting of the elements 136, 135, 133, 134, and 132 is the longest sub-sequence of consecutive integers that is of the length 5. Hence, the output is 5.

## Approach: Using Sorting

We can use the sorting technique using which the consecutive elements will come together. After that, we can run a loop to identify the longest consecutive elements. Observe the following steps.

Step 1: Take two variables, answer and cntConsecutive, and assign the value 0 to these variables.

Step 2: Sort the input inputArr[].

Step 3: Keep the unique elements in the distArr[] array (or an array list) by iterating over the input array inputArr[].

Step 4: Now, iterate the distArr[] array in order to compute the number of consecutive elements. Keep updating the cntConsecutive variable when the previous or the next consecutive element is found. Side by side, keep updating the variable answer.

Observe the implementation of the above steps.

FileName: LongestConsecutiveSubsequence.java

Output:

```For the input array:
11 39 13 10 14 20 12 15
The length of the longest consecutive subsequence is: 6

For the input array:
136 141 156 135 144 133 134 192 143 132 142
The length of the longest consecutive subsequence is: 5
```

Complexity Analysis: Because of sorting, the time complexity of the program is O(N * log(N)). The space complexity of the program is O(N), where N is the total number of elements present in the input array. The space complexity is O(N) because the program is using an array list for storing the distinct elements of the input array.

## Approach: Using Priority Queue

Using a priority queue also, one can find the desired result. Observe the following steps.

Step 1: Make a priority queue for storing the elements. Use a loop and push all the elements in the priority queue. Also, create a variable answer and assign the value 0 to it.

Step 2: Store the first element of the priority queue in a variable called count.

Step 3: Remove it from the Priority Queue

Step 4: Find the difference between the new peek element and the removed first element.

Step 5: If the difference is 1 then increment the count by 1 and repeat step 2 and step 3.

Step 6: If the difference is more than 1, set the counter count to 1 and repeat steps 2 and step 3.

Step 7: If the difference is 0, repeat steps 2 and 3.

Step 8: Compare the value of the count variable with the variable answer and assign the value of the count to the variable answer if its value is greater.

Step 9: Repeat steps 2 to 8 till the priority queue is empty.

Step 10: Return the value of the variable answer.

Let's see the implementation of the above-mentioned steps.

FileName: LongestConsecutiveSubsequence1.java

Output:

```For the input array:
11 39 13 10 14 20 12 15
The length of the longest consecutive subsequence is: 6

For the input array:
136 141 156 135 144 133 134 192 143 132 142
The length of the longest consecutive subsequence is: 5
```

Complexity Analysis: The time complexity, as well as the space complexity of the program, is the same as the previous program.

## Approach: Using HashSet

We know that a hash set can never contain any duplicate elements. Therefore, we put all the elements in a hash set, and the hash set simply discards the duplicate value. Thus, only unique elements remain in a hash set. Now, all we need to do is to find the first element of the consecutive subsequence and then, with the help of the hash set. See the following steps.

Step 1: Create an instance of the hash set that will contain integers. Also, create a variable answer and assign the value zero to it.

Step 2: Insert all the elements of the input array into the hash set.

Step 3: For every element inputArr[i], do the following:

• Check whether the element inputArr[i] is present in the hash set or not.
• If present, then check whether the beginning of the sub-sequence. We can do that by checking whether inputArr[i] - 1 is present or not. If not present, then inputArr[i] is the beginning of the subsequence. If present, then look for another element.
• After finding the first element, use a loop and find whether inputArr[i] + 1 is present or not. If yes, then take a variable whose initial value is zero and increase its count by 1. After the loop ends, compare its value with answerand assign the value to the answer if the value is greater. Also, erase all the elements from the hash set that are traversed by the loop.

Step 4: Return the value of answer variable.

Observe the implementation of the above steps.

FileName: LongestConsecutiveSubsequence1.java

Output:

```For the input array:
11 39 13 10 14 20 12 15
The length of the longest consecutive subsequence is: 6

For the input array:
136 141 156 135 144 133 134 192 143 132 142
The length of the longest consecutive subsequence is: 5
```

Complexity Analysis: The maximum number of times an element can be traversed in the above program is three times. Therefore, the time complexity of the program is O(3 * n), and asymptotically, it is written as O(n), where n is the total number of elements present in the input array. The space complexity of the program is the same as the previous program.

### Feedback   