🚀 Day 70 of #100DaysOfCode Today’s problem was a very popular array + hashing challenge — Longest Consecutive Sequence 🔢 📌 Problem Summary Given an unsorted array of integers, find the length of the longest consecutive elements sequence. The solution must run in O(n) time. 🧠 My Approach: HashSet Optimization Insert all elements into a HashSet for O(1) lookups Only start counting when the current number is the start of a sequence (i.e., num - 1 does NOT exist in the set) Expand forward while consecutive numbers exist Track and update the maximum sequence length This avoids unnecessary re-counting and keeps the solution efficient. ⚙️ Time & Space Complexity ⏱ Time: O(n) 💾 Space: O(n) (HashSet) 🔥 Key Learning Identifying a valid starting point is crucial for optimal solutions. Hashing combined with smart traversal can turn brute-force ideas into clean O(n) logic. Day 70 done ✔️ Onward to Day 71 🚀 #Day70 #100DaysOfCode #LeetCode #Java #HashSet #Arrays #DSA #ProblemSolving #CodingJourney
Longest Consecutive Sequence in O(n) Time with HashSet
More Relevant Posts
-
🚀 𝐃𝐚𝐲 59 / 100 𝐃𝐚𝐲𝐬 𝐨𝐟 𝐂𝐨𝐝𝐞 🚀 Today’s problem: 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 238 – 𝐏𝐫𝐨𝐝𝐮𝐜𝐭 𝐨𝐟 𝐀𝐫𝐫𝐚𝐲 𝐄𝐱𝐜𝐞𝐩𝐭 𝐒𝐞𝐥𝐟 A classic problem that truly tests logical thinking + space-time optimization. 🔹 What I learned & practiced today: 𝐏𝐫𝐞𝐟𝐢𝐱 𝐩𝐫𝐨𝐝𝐮𝐜𝐭 & 𝐬𝐮𝐟𝐟𝐢𝐱 𝐩𝐫𝐨𝐝𝐮𝐜𝐭 technique Solving without 𝐮𝐬𝐢𝐧𝐠 𝐝𝐢𝐯𝐢𝐬𝐢𝐨𝐧 𝐨𝐩𝐞𝐫𝐚𝐭𝐨𝐫 Achieving 𝐎(𝐧) 𝐭𝐢𝐦𝐞 𝐜𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 O(1) extra space optimization (excluding output array) #LeetCode #Java #DSA #Arrays #PrefixSum #ProblemSolving #CodingJourney #Consistency #SoftwareDeveloper #LearningDaily #DeveloperMindset
To view or add a comment, sign in
-
-
#100DaysOfCode 🚀 👉Solved: LeetCode #16 – 3Sum Closest After solving the classic 3Sum problem earlier, this variation pushed me to think slightly differently. Instead of finding an exact triplet sum equal to 0, this time the goal was to find the sum *closest* to a given target. 🔹 Approach: • Sorted the array • Fixed one element at a time • Applied the Two Pointer technique • Continuously tracked the minimum absolute difference Key Insight: Sometimes optimization problems are not about exact matches, but about minimizing deviation. Time Complexity: O(n²) Space Complexity: O(1) This problem strengthened my understanding of: ✔ Two Pointers ✔ Greedy comparison logic ✔ Edge case handling What I learned: Small variations in problem statements can completely change the thinking approach. Classic 3Sum focuses on exact zero. 3Sum Closest focuses on minimizing |sum - target|. #LearnInPublic #DSA #Java #LeetCode #TwoPointers #ProblemSolving #CodingJourney #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 Day 44 Out of #365DayOfCode - LeetCode Today I worked on the Set Matrix Zeroes problem, where the goal is to modify a 2D matrix such that if any element is 0, its entire row and column are set to 0. I implemented an efficient solution with O(m × n) time complexity and optimized space usage by carefully handling matrix traversal and edge cases. This problem helped me strengthen my understanding of: 2D array manipulation In-place updates Optimizing space complexity Handling edge cases in algorithms Consistent practice is helping me improve my problem-solving skills step by step. 🚀 #DSA #Java #ProblemSolving #CodingPractice #365DaysOfCode Github link: https://lnkd.in/gGUy_MKZ
To view or add a comment, sign in
-
-
🚀 100 Day Target – Day 18 Problem: Running Sum of 1D Array Concept: Prefix Sum | Cumulative Computation Today’s focus was on building a running (prefix) sum efficiently in a single pass. Approach: ✔ Traverse once ✔ Keep a cumulative sum ✔ Update in-place Time Complexity: O(n) Space Complexity: O(1) Result? 💯 100% Runtime Performance Sometimes simple problems are about writing clean, efficient logic — not overcomplicating the solution. Sharpening basics. Optimizing thinking. Building speed + clarity. 💪 #100DaysOfCode #Day18 #LeetCode #DSA #Java #PrefixSum #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
Day 17 – LeetCode 1004 | Sliding Window Done Right (O(n) Solution) Day 17 of solving problems consistently. Solved LeetCode 1004 – Max Consecutive Ones III. Problem: Given a binary array, you can flip at most k zeros. Return the maximum number of consecutive 1s possible. Brute force checks every subarray → O(n²) → wasteful. A better approach is Sliding Window + Two Pointers → O(n). Core logic: • Expand window with right pointer • Count zeros • If zeros exceed k → move left pointer • Track max window length This problem is a good reminder that: Constraints usually hint the optimal technique. If you see “subarray + max/min + linear time” → think Sliding Window first. Consistency builds skill. 17 days. No breaks. Code + explanation in the video 👇 #LeetCode #DSA #Algorithms #SlidingWindow #TwoPointers #Java #ProblemSolving #CodingInterview #SoftwareDeveloper #100DaysOfCode #TechJourney
To view or add a comment, sign in
-
Day 16 – LeetCode 1004 | Sliding Window Done Right (O(n) Solution) Day 16 of solving problems consistently. Solved LeetCode 1004 – Max Consecutive Ones III. Problem: Given a binary array, you can flip at most k zeros. Return the maximum number of consecutive 1s possible. Brute force checks every subarray → O(n²) → wasteful. A better approach is Sliding Window + Two Pointers → O(n). Core logic: • Expand window with right pointer • Count zeros • If zeros exceed k → move left pointer • Track max window length This problem is a good reminder that: Constraints usually hint the optimal technique. If you see “subarray + max/min + linear time” → think Sliding Window first. Consistency builds skill. 16 days. No breaks. Code + explanation in the video 👇 #LeetCode #DSA #Algorithms #SlidingWindow #TwoPointers #Java #ProblemSolving #CodingInterview #SoftwareDeveloper #100DaysOfCode #TechJourney
To view or add a comment, sign in
-
Day 15 | LeetCode Daily Solved LeetCode Problem #172 – Factorial Trailing Zeroes Concept: • Counting factors of 5 in factorials • Mathematical optimization over brute force Key Learnings: • Trailing zeroes depend on the number of (2,5) pairs • Counting factors is more efficient than computing factorials Halfway to 30 days — focusing on consistency and clarity in problem solving. Submission link: https://lnkd.in/geCnvxpw #LeetCode #DSA #Math #Java #ProblemSolving
To view or add a comment, sign in
-
-
Even the simplest filtering problems sharpen logical precision. ✅ 🚀 #Day68 of #100DaysOfCodeChallenge Today was about clean iteration and condition-based filtering . 📌 Problem 01: Average Value of Even Numbers That Are Divisible by Three (Easy) Given an array of positive integers, return the average of all even numbers that are divisible by 3. If none exist, return 0. 🧠 Logic: Traverse the array once. Check if a number is divisible by 6 (since even + divisible by 3 ⇒ divisible by 6). Maintain sum and count of valid numbers. If count is 0, return 0. Otherwise, return sum / count (integer division). Day 68 complete ✅ Consistency over intensity. Keep building. 🚀 #100DaysOfCode #Day68 #LeetCode #DSA #Arrays #Math #Java #ProblemSolving #CodingJourney #LearnInPublic #Consistency
To view or add a comment, sign in
-
-
🚀 Day 52/100: Reverse Simulation with Deques Today’s challenge: LeetCode 950 - Reveal Cards In Increasing Order. 🃏 The trick to this problem isn't moving forward; it's thinking backward. To find the original deck order, I reversed the "reveal and move to bottom" process: Sort the deck to know the target reveal order. Reverse the simulation using a Deque (Double-Ended Queue). Shift the last element to the front and add the next sorted card. Key Learning: When a process seems complex to simulate forward, try starting from the result and working back to the beginning! #100DaysOfCode #DSA #LeetCode #Java #ProblemSolving #CodingChallenge
To view or add a comment, sign in
-
-
🚀 Day 19 of My LeetCode Challenge Today’s problem: Find All Duplicates in an Array (LeetCode 442) 🔍 Problem Summary: Given an array of integers where each value appears once or twice and lies between 1 and n, return all elements that appear twice. 🧠 Key Insight: Since numbers are in the range 1 to n, we can use the array itself to track visited elements by marking indices negative. ⚙ Approach (O(n) time | O(1) space): ✔ Traverse the array ✔ Use value → index mapping ✔ If index already negative → duplicate found ⏱ Complexity: ✅ Time: O(n) ✅ Space: O(1) 📌 What I Learned Today: Using index mapping for in-place marking How constraints help optimize space Recognizing patterns for array-based hashing #Day19 #LeetCode #Java #DSA #CodingChallenge #ProblemSolving
To view or add a comment, sign in
-
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development