Longest Consecutive Sequence LeetCode Problem Solved

🚀  Day 29 LeetCode Problem Solved: Longest Consecutive Sequence (128) Today I solved an interesting Data Structures & Algorithms problem on LeetCode. 💻 🔹 Problem: Given an unsorted array of integers, find the length of the longest consecutive elements sequence in O(n) time complexity. 🔹 Example: Input: [100, 4, 200, 1, 3, 2] Output: 4 👉 The longest consecutive sequence is [1,2,3,4]. 🔹 Approach: Instead of sorting the array (which takes O(n log n)), I used a HashSet to achieve O(n) time complexity. ✔ Store all numbers in a HashSet ✔ Identify the start of a sequence (num - 1 not present in the set) ✔ Expand the sequence forward (num + 1, num + 2...) ✔ Track the maximum length 🔹 Complexity: ⏱ Time Complexity: O(n) 📦 Space Complexity: O(n) 💡 Key Learning: Using HashSet efficiently can help optimize problems that involve searching and sequence detection. Excited to keep learning and improving problem-solving skills! 🚀 #leetcode #coding #java #datastructures #algorithms #softwaredeveloper #programming #codingjourney

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories