Longest Consecutive Subsequence
In this class, We discuss the Longest Consecutive Subsequence.
Readers can prepare an entire competitive coding course to crack product development companies. Click Here.
The reader should have basic coding skills to work with competitive coding. Click here.
Question:
Given an array of positive integers.
Find the length of the longest sub-sequence such that the elements in the sequence are consecutive.
Example:
Input: A = [9, 8, 1, 2, 7, 3, 4]
Output: 4
The above elements have sub-sequence 7, 8, 9, and 1,2, 3, 4
The sub-sequence 1, 2, 3, and 4 has the longest length of 4.
The output is 4.
Time complexity: O(N)
Space complexity: O(N)
Logic:
We use a hash table to write a program in the time O(N).
The step-by-step explanation is provided in the video.
Code: