Length of the Longest Run of Consecutive Numbers
Solve this Problemnums, return the length of the longest run of consecutive integers that all appear somewhere in the array. The numbers don't need to be next to each other in the array itself — for [100, 4, 200, 1, 3, 2], the values 1, 2, 3, 4 are each present somewhere, forming a run of length 4.
Checking "is the next number present?" by re-scanning the array every time works, but it's wasteful — the same values get searched for again and again from different starting points. Putting every number into a hash setHash SetA collection that answers "is this value present?" in O(1) time on average, instead of scanning through every element. turns each membership check into O(1). The remaining trick is to only start counting from numbers that are genuine sequence starts — where num - 1 is NOT in the set — so every number in the array is ever counted at most once, keeping the whole algorithm at O(n).
Test Case 1:
Test Case 2:
Test Case 3:
Constraints
- ◆
0 ≤ nums.length ≤ 10⁵ - ◆
-10⁹ ≤ nums[i] ≤ 10⁹
Try the Dry Run
Don't just read the solution — watch it execute, one step at a time.
| 1 | class Solution { |
| 2 | public int longestConsecutiveSequence(int[] nums) { |
| 3 | Set<Integer> numSet = new HashSet<>(); |
| 4 | for (int num : nums) numSet.add(num); |
| 5 | int longest = 0; |
| 6 | for (int num : numSet) { |
| 7 | if (!numSet.contains(num - 1)) { |
| 8 | int current = num; |
| 9 | int length = 1; |
| 10 | while (numSet.contains(current + 1)) { |
| 11 | current++; |
| 12 | length++; |
| 13 | } |
| 14 | longest = Math.max(longest, length); |
| 15 | } |
| 16 | } |
| 17 | return longest; |
| 18 | } |
| 19 | } |
| 20 |
6Put every number into a hash set once: 6 distinct values. Membership checks against this set are O(1).
Approach & Solutions
Brute Force
BruteFor every number in the array, repeatedly check whether the next consecutive number also appears — by scanning the WHOLE array again for each check. Correct, but the same values get re-scanned for over and over, from every possible starting point.
O(n²) to O(n³)O(1)1class Solution {
2 public int longestConsecutiveSequence(int[] nums) {
3 int longest = 0;
4 for (int num : nums) {
5 int current = num;
6 int length = 1;
7 while (contains(nums, current + 1)) {
8 current++;
9 length++;
10 }
11 longest = Math.max(longest, length);
12 }
13 return longest;
14 }
15
16 private boolean contains(int[] nums, int target) {
17 for (int n : nums) {
18 if (n == target) return true;
19 }
20 return false;
21 }
22}Optimal — Hash Set
OptimalPut every number into a hash set once — membership checks become O(1). Then only start counting from numbers that ARE a sequence start (num - 1 is not in the set); every other number will already get counted while extending some earlier run, so skipping them is what keeps the total work at O(n) instead of O(n²).
O(n)O(n)1class Solution {
2 public int longestConsecutiveSequence(int[] nums) {
3 Set<Integer> numSet = new HashSet<>();
4 for (int num : nums) numSet.add(num);
5 int longest = 0;
6 for (int num : numSet) {
7 if (!numSet.contains(num - 1)) {
8 int current = num;
9 int length = 1;
10 while (numSet.contains(current + 1)) {
11 current++;
12 length++;
13 }
14 longest = Math.max(longest, length);
15 }
16 }
17 return longest;
18 }
19}