Longest Consecutive Sequence Problem Statement
You are provided with an unsorted array/list ARR
of N
integers. Your task is to determine the length of the longest consecutive sequence present in the array.
Explanation:
The consecutive sequence should be in the form [NUM, NUM + 1, NUM + 2, ..., NUM + L
] where NUM
is the starting integer of the sequence and L + 1
is the length of the sequence.
Note:
If the array contains any duplicates, consider only one occurrence of the number in the consecutive sequence.
Input:
The first line of input contains a single integer 'T', representing the number of test cases or queries.
The first line of each test case contains integer 'N' denoting the size of the array.
The second line of each test case contains 'N' space-separated integers, the elements of the array.
Output:
For each test case, output a single integer on a new line representing the length of the longest consecutive sequence.
Example:
Input: ARR = [9,5,4,9,10,10,6]
Output: 3
Explanation: The longest consecutive sequence is [4,5,6].
Constraints:
1 <= T <= 10
1 <= N <= 10^5
-10^9 <= ARR[i] <= 10^9
Note:
You do not need to print the expected output; it's already handled. Just implement the required function.
Performance consideration:
Can you solve this problem with O(N) time complexity and O(N) space complexity?
Aníl kumar
3mo
create an array of size maximum element then mark all by 1, in new array now find longest subarray which has maximum 1 in new array...for give eg create array of size 10, 0 1 2 3 4 5 6 7 8 9 10, now e...read more
Help your peers!
Add answer anonymously...
Top Paytm Software Engineer interview questions & answers
Popular interview questions of Software Engineer
Top HR questions asked in Paytm Software Engineer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app