Longest Consecutive Sequence Problem Solved in O(n) Time

🚀 Day 21 of solving DSA problems 🧠 Problem: Longest Consecutive Sequence Today I solved a problem where we must find the length of the longest sequence of consecutive numbers in an unsorted array — in O(n) time. 💡 Key Insight: Instead of sorting, we store elements in a HashSet and only start counting when a number has no previous consecutive element (num - 1 not present). 📌 Pattern Learned: 👉 Sequence detection using Hashing Use a set for O(1) lookup and avoid unnecessary repeated checks. ⏱ Complexity: Time → O(n) Space → O(n) 🔥 Lesson: Sometimes the optimal solution doesn’t involve sorting — smart data structures can reduce time complexity drastically. #DSA #Java #ProblemSolving #CodingJourney #LearningInPublic

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories