...

/

Longest Consecutive Sequence

Longest Consecutive Sequence

Try to solve the Longest Consecutive Sequence problem.

Statement

Given an unsorted array, nums, your task is to return the length of the longest consecutive sequence of elements. The consecutive sequence of elements is such that there are no missing elements in the sequence. The consecutive elements can be present anywhere in the input array.

Note: Two elements, xx and yy, are called consecutive if the difference between them is equal to 11.

Constraints:

  • 00 \leq nums.lengths 103\leq 10^{3}
  • 106-10^{6} \leq nums[i] 106\leq 10^{6}

Examples

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Longest Consecutive Sequence

1.

What is the output if the following numbers array is given as input?

nums = [1,3,2][1, 3, 2]

A.

11

B.

00

C.

33

D.

44


1 / 4

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4

Try it yourself

Implement your solution in main.cpp in the following coding playground. The supporting code template provided in UnionFind.cpp is meant to assist in developing your solution to the problem.

C++
usercode > main.cpp
/*
⬅️ We have provided a UnionFind.cpp file under the "Files" tab
of this widget. You can use this file to build your solution.
*/
#include "UnionFind.cpp"
int LongestConsecutiveSequence(std::vector<int>& nums) {
// Replace this placeholder return statement with your code
return 0;
}
Longest Consecutive Sequence

Access this course and 1200+ top-rated courses and projects.