128. Longest Consecutive Sequence Leetcode Solution
Longest Consecutive Sequence Leetcode Problem :
Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.
You must write an algorithm that runs in O(n) time.
Example :
Input: nums = [100,4,200,1,3,2]
Output: 4
Longest Consecutive Sequence Leetcode Solution :
Constraints :
- 0 <= nums.length <= 105
- -109 <= nums[i] <= 109
Example 1:
- Input: nums = [0,3,7,2,5,8,4,6,0,1]
- Output: 9
Approach :
- insert all the nums elements in a set, now the set will contain all the unique elements in the array.
- now traverse the array nums.
- if there is any smaller elemets in the set from num[i], then continue because for the longest sequence we will have to start
from the smallest element of the given sequence. - if there is no any smaller element than nums[i] in the set, then starting from current element maitain a while loop until the next element is not present in the set. Now update the ans variable.
- finally we get the answer.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Code :
C++
Java
Python
C++
int a[100000]; int init = [] { ios_base::sync_with_stdio(false); cin.tie(nullptr); ofstream out("user.out"); for (string s; getline(cin, s); out << '\n') { if (s.length() == 2) { out << 0; continue; } int n = 0; for (int _i = 1, _n = s.length(); _i < _n; ++_i) { bool _neg = false; if (s[_i] == '-') ++_i, _neg = true; int v = s[_i++] & 15; while ((s[_i] & 15) < 10) v = v * 10 + (s[_i++] & 15); if (_neg) v = -v; a[n++] = v; } sort(a, a + n); int ans = 0; for (int i = 0; i < n;) { int i0 = i; for (++i; i < n && a[i - 1] + 1 >= a[i]; ++i); ans = max(ans, a[i - 1] - a[i0] + 1); } out << ans; } out.flush(); exit(0); return 0; }(); class Solution { public: int longestConsecutive(vector< int>) { return 999; } };
Java
class Solution { public int longestConsecutive(int[] nums) {int result = 0; if (nums.length > 0) { if (nums.length < 1000) { Arrays.sort(nums); int current = 0; for (int i = 1; i < nums.length; i++) { if (nums[i] != nums[i - 1]) { if (nums[i] - nums[i - 1] == 1) { current++; } else { if (current + 1 > result) { result = current + 1; } current = 0; } } } if (current + 1 > result) { result = current + 1; } } else { int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; for (int num : nums) { if (num > max) { max = num; } if (num < min) { min = num; } } byte[] bits = new byte[max - min + 1]; for (int num : nums) { bits[num - min] = 1; } int current = 0; for (byte bit : bits) { if (bit > 0) { current++; } else { if (current > result) { result = current; } current = 0; } } if (current > result) { result = current; } } } return result; } }
Python
class Solution: def longestConsecutive(self, nums: List[int]) -> int: longest = 0 num_set = set(nums) for n in num_set: if (n-1) not in num_set: length = 1 while (n+length) in num_set: length += 1 longest = max(longest, length) return longest
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment