🚀 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
Longest Consecutive Sequence Problem Solved in O(n) Time
More Relevant Posts
-
🚀 Day 27 of Solving DSA Problems 🧠 Problem: Majority Element (> n/3 times) Today I learned an advanced version of the Boyer–Moore Voting Algorithm. 💡 Key Insight: If an element appears more than ⌊n/3⌋ times, there can be at most 2 such elements. So instead of using a HashMap, we can track only two candidates and cancel out different elements while traversing. After one pass, we verify the candidates’ counts to confirm. ⏱ Complexity: Time → O(n) Space → O(1) 🔥 Lesson: Sometimes you don’t need extra memory — smart logic and observation can replace data structures. Understanding patterns > memorizing solutions 📈 #DSA #Java #ProblemSolving #CodingJourney #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 22 of solving DSA problems 🧠 Problem: Set Matrix Zeroes (2D Array) Today I worked on a matrix problem where if any element is 0, its entire row and column must be set to 0 — and the catch was to do it in-place. 💡 Key Insight: Instead of modifying the matrix immediately (which breaks future checks), we first mark which rows and columns should become zero, then update them later. 📌 Pattern Learned: 👉 Mark first → Modify later This pattern is useful whenever changing data early can affect future logic. ⏱ Complexity: Time → O(m × n) Space → O(1) (optimized approach) 🔥 Lesson: Many matrix problems test careful thinking more than coding difficulty. Handling indices and update order correctly is the real challenge. #DSA #Java #CodingJourney #ProblemSolving #LearningInPublic
To view or add a comment, sign in
-
-
DSA Journey — Day 22 Today I solved the Balanced Binary Tree problem and learned an important optimization concept. -> Problem: Check whether a binary tree is height-balanced (difference between left and right subtree height ≤ 1 for every node). -> Key Learning: Instead of calculating height separately for each node (which gives O(n²) complexity), we can combine height calculation + balance checking in a single DFS traversal to achieve O(n) time. Core Insight: Return -1 immediately when a subtree is unbalanced. This avoids unnecessary computations and makes the solution efficient. Why this matters: This problem taught me how to: Optimize brute force recursion Detect early stopping conditions Write cleaner recursive logic Growth Mindset: Every tree problem is improving my recursion intuition and problem-solving speed. #DSA #Java #CodingJourney #Recursion #LearningInPublic #WomenInTech
To view or add a comment, sign in
-
🚀 Day 23 of solving DSA problems 🧠 Problem: Rotate Image (Matrix) by 90° Clockwise — In-Place Today I learned a powerful matrix trick. At first, I solved it using an extra matrix (brute force). But then I understood the optimal approach: 👉 Transpose the matrix 👉 Reverse each row This converts the matrix into a 90° rotated version without using extra space. ⏱ Complexity Time → O(n²) Space → O(1) 💡 Lesson: Whenever a problem says “do it in-place”, think about patterns like swapping, reversing, or transposing instead of using extra memory. Consistency + learning from mistakes = growth 📈 #DSA #Java #CodingJourney #ProblemSolving #LearningInPublic
To view or add a comment, sign in
-
-
Day 21/100 of DSA , (Arrays) 🚀Today I tackled the “Set Matrix Zeroes” problem At first, the brute force approach is tempting: traverse the matrix and use extra space to track zeros. Instead, I focused on the logic behind minimizing space: 🔹 Using the first row and first column as markers 🔹 Traversing carefully to avoid overwriting important values 🔹 Setting zeros in-place without extra arrays This problem is a reminder that optimization is not just about speed, but also about smart memory usage. 💡 Key takeaways: Think about what can be reused instead of creating new structures Always consider edge cases in 2D arrays Small logical adjustments make a big difference in performance Another step forward in mastering DSA fundamentals and improving problem-solving efficiency. 🚀 #DSA #Java #ProblemSolving #MatrixProblems #CodingJourney #InterviewPreparation #OptimizedCode
To view or add a comment, sign in
-
-
Imagine finding the longest number streak in an unsorted array. For example: [100, 4, 200, 1, 3, 2] Can you spot the longest consecutive sequence? 🚀 Day 67/365 — DSA Challenge Solved: Longest Consecutive Sequence The goal: Find the length of the longest sequence of consecutive numbers. Example: Input [100,4,200,1,3,2] Consecutive sequence: [1,2,3,4] Output: 4 💡 My Approach I solved it in two main steps. Step 1 — Sort the array Since the numbers are unsorted, I first sorted the array using bubble sort. Example after sorting: [1,2,3,4,100,200] Step 2 — Count consecutive numbers Then I looped through the array: • If current number = previous + 1 → increase count • If numbers are equal → skip duplicates • Otherwise → reset count While traversing, I kept track of the longest sequence length. This problem reminded me: Sometimes sorting first makes pattern detection much easier. Day 67/365 complete. Code 👇 https://lnkd.in/dad5sZfu #DSA #Java #LeetCode #LearningInPublic #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Day 33 of DSA 🚀 | Binary Search Today I worked on one of the most fundamental algorithms in problem solving — Binary Search. 🔹 Task: Given a sorted array, find the index of a target element in O(log n) time. 🔹 Key realization: Binary Search is not about guessing. It’s about eliminating half of the search space at every step. 💡 What I learned: Binary Search works only on sorted arrays Using left, right, and mid pointers helps narrow the search efficiently Calculating mid as left + (right - left) / 2 avoids overflow Clear boundary conditions (left <= right) are critical ⏱ Time Complexity: O(log n) 📦 Space Complexity: O(1) This problem reinforced how mastering basics builds the foundation for advanced algorithms. #DSA #BinarySearch #Java #ProblemSolving #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
🔥 Day 353 – Daily DSA Challenge! 🔥 Problem: 🔍 Single Element in a Sorted Array Given a sorted array where every element appears twice except one, find that single element in O(log n) time. 💡 Key Insight — Binary Search on Pairs In a perfect paired array: Pairs start at even indices Pattern breaks at the single element We use binary search to detect where this pattern breaks. 🧠 Core Observation Before the single element: pairs → (even, odd) After the single element: pairs shift → (odd, even) So we normalize mid: if mid is odd → make it even ⚡ Algorithm Logic ✅ Find mid ✅ Make mid even ✅ Compare: If nums[mid] == nums[mid+1] → move right Else → move left This narrows down to the single element. ⚙️ Complexity ✅ Time Complexity: O(log n) ✅ Space Complexity: O(1) 💬 Challenge for you 1️⃣ Why do we force mid to be even? 2️⃣ How would you solve this using XOR in O(n)? 3️⃣ What if elements appear thrice except one? #DSA #Day353 #LeetCode #BinarySearch #Arrays #Optimization #Java #ProblemSolving #KeepCoding
To view or add a comment, sign in
-
-
Day 48 – DSA Journey 🚀 | Smallest Number With All Set Bits Continuing my daily DSA practice, today I solved a bit manipulation problem that highlights the importance of recognizing patterns in binary numbers. 📌 Problem Practiced: Smallest Number With All Set Bits (LeetCode 3370) 🔍 Problem Idea: Given a number n, find the smallest number x ≥ n whose binary representation contains **only 1s`. 💡 Key Insight: Numbers with all set bits follow the pattern 2^k − 1 (e.g., 1, 3, 7, 15...). We can generate these numbers directly using bit manipulation instead of checking every value. 📌 Concepts Strengthened: • Bit shifting (<<) • Bitwise OR (|) • Binary pattern recognition ⏱️ Time Complexity: O(log n) 📦 Space Complexity: O(1) Today’s takeaway: Recognizing binary patterns can simplify many bit manipulation problems and lead to cleaner solutions. On to Day 49! 🚀 #Day48 #DSAJourney #LeetCode #BitManipulation #Java #ProblemSolving #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
Solved: Invert Binary Tree (BFS Approach) Worked on Invert Binary Tree using an iterative Queue (BFS) approach. 🔹 Idea: Traverse the tree level by level and swap the left and right children of every node. 🔹 Key Learning: Tree inversion is a structural transformation, not just swapping values. We must swap the child references, not node data. 🔹 Approach: Use a Queue for level-order traversal For each node: Swap left and right Add children to queue ⏱ Time Complexity: O(n) 📦 Space Complexity: O(n) Great practice for understanding: BFS vs DFS in trees Reference handling in Java Structural vs value-based operations Consistent practice on tree problems is sharpening my DSA fundamentals 🚀 #DSA #Java #BinaryTree #CodingPractice #LeetCode
To view or add a comment, sign in
-
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