🔥 Day 36/100 of #100DaysOfCode - Matrix Zero Transformation! Today's Problem: Set Matrix Zeroes Task: Given an m x n matrix, if an element is 0, set its entire row and column to 0s. Must be done in-place. Solution: Used the optimal O(1) space approach! Leveraged the first row and first column as markers to track which rows/columns need to be zeroed, with a separate variable for column 0. Key Steps: Mark zeros in first row/column while preserving original state Process inner matrix using the markers Zero rows/columns based on markers Smart Optimizations: Uses matrix itself for storage instead of extra O(m+n) space Handles edge case for first column separately Single pass for marking + two passes for execution Advanced matrix manipulation that demonstrates deep understanding of in-place algorithms! Perfect example of trading complexity for optimal space usage. 🧩 #100DaysOfCode #LeetCode #Java #Algorithms #Matrix #InPlace #CodingInterview
Set Matrix Zeroes in-place with O(1) space
More Relevant Posts
-
🔥 Day 115 of My DSA Challenge – Intersection of Two Arrays II 🔷 Problem : 350. Intersection of Two Arrays II 🔷 Goal : Return the intersection of two arrays, where each element in the result must appear as many times as it shows in both arrays. Order doesn’t matter. 🔷 Key Insight : This is an extension of the previous problem (Intersection of Two Arrays). The twist? Here we need to consider duplicate occurrences too. For example : nums1 = [1,2,2,1], nums2 = [2,2] → result should be [2,2] (because 2 appears twice in both). 🔷 Approach : 1️⃣ Use a HashMap to count occurrences of each element in nums1. 2️⃣ Traverse nums2 and check if an element exists in the map. 3️⃣ If yes → add it to the result and decrease its count in the map. 4️⃣ When the count becomes 0, remove it from the map to keep things clean. Time Complexity: O(n + m) Space Complexity: O(n) This problem strengthens hash-based frequency counting and element tracking. Use a map to record frequency — then match, decrease, and clean up. This logic is key for : ✅ Counting duplicates ✅ Array frequency comparison ✅ Inventory & matching systems Every small concept compounds into bigger problem-solving clarity. Step by step, I’m becoming stronger at thinking like a coder 💪💻 #Day115 #DSA #100DaysOfCode #HashMap #LeetCode #ProblemSolving #Java #CodingChallenge #Algorithms #DataStructures #DeveloperJourney #LogicBuilding #EngineerMindset #GrowEveryday
To view or add a comment, sign in
-
-
🔥 Day 35/100 of #100DaysOfCode - Finding Longest Sequence! Today's Problem: Longest Consecutive Sequence Task: Find the length of the longest consecutive elements sequence in an unsorted array. Solution: Used a HashSet for O(1) lookups! First added all elements to the set, then for each number, checked if it's the start of a sequence (no num-1 in set). If yes, counted consecutive numbers ahead. Key Insights: O(n) time complexity by only checking sequence starters HashSet eliminates duplicates and provides fast lookups Avoids O(n log n) sorting approach Smart Optimization: Only begins counting from sequence starting points Each number is processed at most twice (visited in set iteration + sequence counting) Handles duplicates and empty arrays gracefully Elegant solution that transforms an O(n²) brute force into O(n) using smart data structure choice! 💡 #100DaysOfCode #LeetCode #Java #Algorithms #HashSet #Arrays #CodingInterview
To view or add a comment, sign in
-
-
🌟 Day 76 of #100DaysOfCodingChallenge 📘 Problem: LeetCode 73 – Set Matrix Zeroes Today, I explored an important array manipulation problem — setting entire rows and columns to zero if any element in a 2D matrix is zero. 🧩 Problem Understanding Given an m x n matrix, if any cell has the value 0, we must make its entire row and column 0. For example: Input: [ [1, 2, 3], [4, 0, 6], [7, 8, 9] ] Output: [ [1, 0, 3], [0, 0, 0], [7, 0, 9] ] 💡 My Approach (Brute Force) To handle this, I used two extra arrays — one to track rows and another to track columns that should become zero. 🪄 Steps followed: 1️⃣ Traverse the matrix and store the indices of rows and columns containing 0. 2️⃣ Loop through the matrix again and make the elements 0 wherever their row or column index is marked. This simple approach helps clearly visualize how matrix updates propagate. 🧠 Concept Learned: How to access and manipulate specific rows and columns in a 2D array. The importance of auxiliary data structures (like boolean arrays) in preserving information during traversal. 🕒 Complexity: Time: O(m × n) Space: O(m + n) 🧵 Next Step: I’ll be optimizing this approach to an O(1) space solution using the matrix itself as a marker — a neat trick in in-place algorithms! #100DaysOfCode #Day75 #LeetCode #Java #CodingChallenge #ProblemSolving #ArrayManipulation #DataStructures #WomenInTech
To view or add a comment, sign in
-
-
#Day_23 💡 Problem Solved: Intersection of Two Arrays Today’s challenge tested my ability to handle array manipulation and two-pointer logic efficiently. 🧩 Problem Overview: Given two integer arrays, the task is to find their intersection — meaning the common elements between both arrays, including duplicates. For example: nums1 = [4,9,5] and nums2 = [9,4,9,8,4] 👉 Output should be [4,9] (since both 4 and 9 appear in both arrays). ⚙️ My Approach: Sorting both arrays: By sorting nums1 and nums2, I was able to efficiently compare elements in a linear scan. Two-pointer technique: I used two indices i and j, starting from 0 in each array. If both elements matched → it’s part of the intersection. If one was smaller → move that pointer ahead to catch up. Storing results dynamically: Added all matching elements into an ArrayList. Finally converted it into an integer array for the result. This approach ensures O(n log n) time complexity due to sorting, but O(n) in space — a solid balance between clarity and performance. 🧠 Key Takeaways: Sorting simplifies complex comparison problems. The two-pointer pattern is extremely useful for dealing with sorted arrays. Always remember: clean logic > brute force. 🔥 Every new problem brings a fresh perspective on optimizing logic and learning better ways to handle data structures. #Java #CodingChallenge #LeetCode #100DaysOfCode #ProblemSolving #DSA #ProgrammingJourney #TechWithMudassir
To view or add a comment, sign in
-
-
🚀 Day 406 of #500DaysOfCode 🎯 Problem: 3318. Find X-Sum of All K-Long Subarrays I 🧩 Difficulty: Easy Today’s challenge focused on computing the X-Sum for every subarray of length k. The X-Sum involves identifying the top X most frequent elements in each subarray — and if frequencies tie, we prioritize larger values. Then, we sum up the occurrences of only those selected elements 💡 💻 Approach: For each subarray of length k, count the frequency of elements. Sort elements by frequency (descending) and value (descending). Pick the top x elements and sum their occurrences in the subarray. Store this result for each subarray. 🧠 Key Concepts Used: Frequency counting using HashMap Custom sorting with comparator logic Sliding subarray analysis 📊 Complexity: O(n × k × log k) 🔍 Topics: Arrays | HashMap | Sorting Every problem solved adds a new pattern to how we think about optimization and logical structuring in code 💪 #Java #LeetCode #ProblemSolving #CodingChallenge #DataStructures #Algorithms #100DaysOfCode #500DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 45 of #100DaysOfCode – Striver’s DSA Sheet 🚀 ✅ Topic Covered Today: Implement Queue Using Array 💡 Lesson of the Day (Approach-Focused): 🔹 Understanding the Core Idea A queue follows FIFO (First In, First Out) order. Using an array, we simulate this behavior by maintaining: front pointer (f) → points to the first element rear pointer (r) → points to the last inserted element size / capacity → fixed array length 🔹 1️⃣ Enqueue (Insert) Check if queue is full → rear == size - 1 Increment rear Insert element at arr[rear] 🧮 Time Complexity: O(1) 💾 Space Complexity: O(1) extra 🔹 2️⃣ Dequeue (Remove) Check if queue is empty → front == -1 Return element at arr[front] Move front forward If queue becomes empty again → reset front = rear = -1 🧮 Time Complexity: O(1) 🔹 3️⃣ Peek Return arr[front] without removing it. 🧮 Time Complexity: O(1) 🔹 4️⃣ isEmpty / isFull isEmpty: front == -1 isFull: rear == size - 1 ✨ Key Idea: Implementing a queue using an array helps understand pointer movement, memory management, and why sometimes a circular queue is needed when fixed arrays cause unused space. 💭 Learning: Today strengthened my understanding of how queues work under the hood and how core operations actually use pointer updates — a great foundation before moving to circular queues and deque structures. #100DaysOfCode #DSA #StriversSheet #Java #Queue #Arrays #ProblemSolving #CodingJourney #LogicBuilding #Consistency
To view or add a comment, sign in
-
-
🚀 Day 62 – In-Place Array Filtering with Clean Two-Pointer Logic 🧩 Problem: 27. Remove Element The task is to remove all occurrences of a given value from the array in-place and return the new valid length. Only the first k elements matter after removal, and we must avoid using extra space making this a good test of controlled index manipulation. 🧠 Approach To solve this efficiently, I used an insertion-based two-pointer strategy, which ensures both clarity and optimal performance. I maintained an insertPos pointer that always marks the next position where a valid element should be placed. As I iterated through the array, every element that wasn’t equal to the given value was copied to this position, and the pointer moved forward. This approach ensures: 👉 A single linear pass through the array 👉 Strict in-place modification 👉 Clean separation of valid and removed elements Full control over the final valid length The resulting length is simply the total size minus the number of removed elements, giving a direct and efficient solution. This method clearly shows structured thinking, control over memory usage, and the ability to simplify a problem into predictable pointer movements — exactly what strong algorithmic reasoning looks like. 🔗 Problem Link: https://lnkd.in/gAakjJqC 🔗 GitHub Link: https://lnkd.in/gCe-A-Ev 💡 Reflection: Working on this problem reminded me how powerful a simple pointer and a clear goal can be. Not every challenge needs complex logic sometimes the cleanest solutions come from knowing exactly how you want the data to look and moving toward it step by step. This problem reinforced the value of in-place changes, memory efficiency, and how clean thinking leads to clean code. #LeetCode #Arrays #TwoPointers #InPlaceAlgorithm #Java #Day62 #CodingJourney #100DaysChallenge #ProblemSolving #LearningEveryday #CleanCode
To view or add a comment, sign in
-
-
💻 Day 62 of #LeetCode100DaysChallenge Solved LeetCode 142: Linked List Cycle II — a problem that deepens understanding of linked list traversal and cycle detection using pointers. 🧩 Problem: Given the head of a linked list, return the node where the cycle begins. If there’s no cycle, return null. A cycle exists if a node can be revisited by continuously following the next pointer. The goal is to identify the exact node where the cycle starts — without modifying the list. 💡 Approach — Floyd’s Cycle Detection (Tortoise and Hare): 1️⃣ Use two pointers — slow and fast. 2️⃣ Move slow by 1 step and fast by 2 steps until they meet (if a cycle exists). 3️⃣ Once they meet, reset one pointer to the head. 4️⃣ Move both one step at a time; the node where they meet again is the start of the cycle. 5️⃣ If no meeting point (i.e., fast or fast.next becomes null), return null. ⚙️ Complexity: Time: O(N) — each pointer traverses the list at most twice. Space: O(1) — no extra memory used. ✨ Key Takeaways: ✅ Strengthened Floyd’s algorithm understanding for detecting and locating cycles. ✅ Learned how mathematical reasoning helps pinpoint the start node of a loop. ✅ Reinforced efficient use of pointer manipulation in linked list problems. #LeetCode #100DaysOfCode #Java #LinkedList #CycleDetection #FloydsAlgorithm #DSA #CodingJourney #WomenInTech #ProblemSolving
To view or add a comment, sign in
-
-
🌟 Day 38 of #100DaysOfCode 🌟 🔍 Exploring Pivot Index & Equilibrium — Balancing Logic with Precision 🔹 What I Solved Today’s challenge focused on finding the pivot index in an array — the position where the sum of all elements to the left equals the sum of all elements to the right. A perfect blend of logic, arithmetic reasoning, and optimization — a reminder that balance matters in both code and life 🧮 Problem: Find the Pivot Index 💡 Example: Input: [1,7,3,6,5,6] → Output: 3 Explanation: Left Sum = 11, Right Sum = 11 → Pivot = 3 🧠 Concepts Used Prefix Sum Logic Iteration & Accumulation Conditional Checking Time Complexity → O(n) ⚙️ Approach 1️⃣ Compute total sum of array 2️⃣ Initialize leftSum = 0 3️⃣ Traverse each index: • If leftSum == totalSum - leftSum - nums[i], return i • Else, add nums[i] to leftSum 4️⃣ Return -1 if no pivot found 🚀 What I Learned ✨ Prefix sums simplify comparisons beautifully ✨ Even small problems teach deep optimization ✨ Balance between simplicity and efficiency is key 💬 Reflection Today’s task was about equilibrium — in arrays and in mindset. Sometimes the calmest logic wins. #100DaysOfCode #Day38 #Java #Array #ProblemSolving #Algorithm
To view or add a comment, sign in
-
-
#Day_24 💡 Problem Solved: Intersection of Two Arrays II Today’s challenge tested my ability to handle array manipulation and two-pointer logic efficiently. 🧩 Problem Overview: Given two integer arrays, the task is to find their intersection — meaning the common elements between both arrays, including duplicates. For example: nums1 = [4,9,5] and nums2 = [9,4,9,8,4] 👉 Output should be [4,9] (since both 4 and 9 appear in both arrays). ⚙️ My Approach: Sorting both arrays: By sorting nums1 and nums2, I was able to efficiently compare elements in a linear scan. Two-pointer technique: I used two indices i and j, starting from 0 in each array. If both elements matched → it’s part of the intersection. If one was smaller → move that pointer ahead to catch up. Storing results dynamically: Added all matching elements into an ArrayList. Finally converted it into an integer array for the result. This approach ensures O(n log n) time complexity due to sorting, but O(n) in space — a solid balance between clarity and performance. 🧠 Key Takeaways: Sorting simplifies complex comparison problems. The two-pointer pattern is extremely useful for dealing with sorted arrays. Always remember: clean logic > brute force. 🔥 Every new problem brings a fresh perspective on optimizing logic and learning better ways to handle data structures. #Java #CodingChallenge #LeetCode #100DaysOfCode #ProblemSolving #DSA #ProgrammingJourney #TechWithMudassir
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
Amazing consistency ✨