i am doing dry run on the code but getting confused. can u please dry any input on this code .
Longest Consecutive Sequence implementation
Create an empty hash.
Insert all array elements to hash.
Do following for every element arr[i]
Check if this element is the starting point of a subsequence. To check this, we simply look for arr[i] – 1 in the hash, if not found, then this is the first element a subsequence.
If this element is the first element, then count number of elements in the consecutive starting with this element. Iterate from arr[i] + 1 till the last element that can be found.
If the count is more than the previous longest subsequence found, then update this.
eg: 1, 9, 3, 10, 4, 20, 2
insert all elements into a set ( sorted order is obtained)
iterate over the set:
We start with a empty Ordered hash map
add 1 we check if 0 is present
if present max_sequence = max(max_sequence, hash[a[i]-1]+1 )
else put a[i] in map and corresponding freq in 1
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.