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, and , are called consecutive if the difference between them is equal to .
Constraints:
-
nums.lengths
-
nums[i]
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
What is the output if the following numbers array is given as input?
nums =
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.
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.
/*⬅️ We have provided a UnionFind.cpp file under the "Files" tabof 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 codereturn 0;}