Day 39: Longest Consecutive Sequence in Array Problem

🚀 #100DaysOfCode – Day 39 Update Today I solved an interesting array problem. 🔹 Problem: Longest Consecutive Sequence in an Array ⸻ ✅ Approach 1 — Brute Force 📌 Core Idea: • Pick each element and try to build a consecutive sequence from it. • For every element x, check if x + 1, x + 2, … exists in the array. ⸻ ✅ Algorithm Steps: 1️⃣ Traverse each element in the array. 2️⃣ For every element, initialize count = 1 and current = arr[i]. 3️⃣ Run a loop to check if current + 1 exists in the array. 4️⃣ If found: • Increment count • Update current = current + 1 5️⃣ After the loop, update the maximum length. ⸻ ⏱ Time Complexity: O(N²) (due to repeated searching) 💾 Space Complexity: O(1) ⸻ 💡 Key Learning: Brute force works but is inefficient due to repeated searches. Next step is to optimize using sorting or HashSet for better performance. #Day39 #100DaysOfCode #DSA #Java #Arrays #ProblemSolving #CodingJourney

To view or add a comment, sign in

Explore content categories