Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Longest Consecutive Sequence Editorial

DSA Editorial, Solution and Code

Practice Problem Link: https://workat.tech/problem-solving/practice/longest-consecutive-sequence

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given an array of integers A, find the length of the longest consecutive elements sequence.

Naive Approach

The simple solution is to sort the array and find the longest consecutive sequence in the sorted array.

  • Initialize the longest sequence as 1.
  • Traverse the array from the index 1 to n - 1.
  • If the difference between the current element and the previous element is greater than 1 then set the value of the longest sequence as 1.
  • Otherwise, if the difference between two consecutive elements is 1, increment the value of the longest sequence by 1 and update the maximum value of the longest sequence.

Analysis

  • Time Complexity: O(n * log(n))
  • Auxiliary Space Complexity: O(1)

Implementation

C++
int longestConsecutiveSequence(vector<int> &A) {
    sort (A.begin(), A.end());
	int currentMaxSequence = 1;
	int ans = 1;
	for (int i = 1; i < A.size(); i++) {
		if (A[i] - A[i - 1] > 1) {
			currentMaxSequence = 1;
		} else {
			if (A[i] - A[i - 1] == 1) {
				currentMaxSequence++;
			}
		}
		ans = max (ans, currentMaxSequence);

	}
	return ans;
}
Java
class Solution {
	int longestConsecutiveSequence(int[] A) {
	    Arrays.sort (A);
		int currentMaxSequence = 1;
		int ans = 1;
		for (int i = 1; i < A.length; i++) {
			if (A[i] - A[i - 1] > 1) {
				currentMaxSequence = 1;
			} else {
				if (A[i] - A[i - 1] == 1) {
					currentMaxSequence++;
				}
			}
			ans = Math.max (ans, currentMaxSequence);

		}
		return ans;
	}
}

Optimal Approach

This problem can be solved efficiently by using a hashmap.

  • Create a hashmap, say countAllElements.
  • Traverse the array and for every valid i set countAllElements[A[i]] = 1;
  • Now again traverse the array and for each element check if the previous element is present in the hashmap.
  • If the previous element is present in the hashmap then this element can not be a starting point.
  • If the previous element is not present in the hashmap then the current element must be a starting point of a consecutive sequence. Count the number of elements in the current sequence from the hashmap and update the maximum value of the longest sequence.

Analysis

  • Time Complexity: O(n)
  • Auxiliary Space Complexity: O(n)

Implementation

C++
int longestConsecutiveSequence(vector<int> &A) {
    unordered_map<int, int> countAllElements;
	int n = A.size();
	for (int i = 0; i < n; i++) {
		countAllElements[A[i]] = 1;
	}
	int ans = 0, currentSequence = 0;
	for (int i = 0; i < n; i++) {
		if (countAllElements[A[i] - 1] == 1) {
		    continue;
		} 
		currentSequence = 0;
		int value = A[i];
		while (countAllElements[value] == 1) {
			currentSequence++;
			value++;
		}
		ans = max (ans, currentSequence);
	}
	return ans;
}
Java
class Solution {
	int longestConsecutiveSequence(int[] A) {
	    HashMap <Integer, Integer> countAllElements = new HashMap<Integer, Integer> ();
		int n = A.length;
		for (int i = 0; i < n; i++) {
			countAllElements.put(A[i], 1);
		}
		int ans = 0, currentSequence = 0;
		for (int i = 0; i < n; i++) {
			if (countAllElements.get(A[i] - 1) != null) {
				continue;
			} 
			currentSequence = 0;
			int value = A[i];
			while (countAllElements.get(value) != null) {
				currentSequence++;
				value++;
			}
			ans = Math.max (ans, currentSequence);
		}
		return ans;
	}
}
Related Content
Clone List with Random Pointer
Distinct Numbers in Window
Four Sum
Identical Twins
Implement Counting Sort
Longest Subarray with Zero Sum
Longest Substring with K Unique Characters
Longest Substring Without Repeat
LRU Cache
Non-Repeating Element
Primes upto N
Subarrays With Given XOR
Three Sum
Two Sum
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet