🔥 Day 116 of My DSA Challenge – Move Zeroes 🔷 Problem : 283. Move Zeroes 🔷 Goal : Move all 0s to the end of the array while maintaining the relative order of non-zero elements. Must be done in-place without using extra space. 🔷 Key Insight : This problem focuses on in-place array manipulation and pointer logic. We need to keep all non-zero elements in front and shift all zeros toward the end — but without disturbing their relative order. A simple and efficient solution is to use the two-pointer approach: One pointer (idx) scans through the array. Another pointer (k) keeps track of where the next non-zero should be placed. 🔷 Approach : 1️⃣ Initialize pointer k = 0 2️⃣ Traverse the array with index idx 3️⃣ If nums[idx] != 0, assign nums[k] = nums[idx] If k != idx, set nums[idx] = 0 to push zero backward Increment k 4️⃣ The array is now rearranged in-place Time Complexity: O(n) Space Complexity: O(1) This problem strengthens array manipulation and in-place optimization skills. Use pointers wisely — move only what’s needed, keep it clean, keep it fast. This logic is useful for : ✅ Partitioning problems ✅ Rearranging arrays ✅ In-place sorting optimizations Another small but meaningful win — one step closer to mastering the art of clean, efficient code 🚀 #Day116 #DSA #100DaysOfCode #LeetCode #TwoPointers #Arrays #ProblemSolving #Java #CodingChallenge #Algorithms #DataStructures #DeveloperJourney #EngineerMindset #LogicBuilding #GrowEveryday
How to Move Zeroes to the End of an Array in Java
More Relevant Posts
-
🔥 Day 118 of My DSA Challenge – Sort Array By Parity II 🔷 Problem : 922. Sort Array By Parity II 🔷 Goal : Rearrange the array so that — ✅ At even indices, we place even numbers ✅ At odd indices, we place odd numbers Given condition : Half of the numbers are even, half are odd — so a valid arrangement is always possible. 🔷 Key Insight : This problem is a perfect example of pointer-based placement. We don’t need to sort — we just need to place: Even numbers → even positions Odd numbers → odd positions We can do this in one pass by using two pointers: evenP → starts at index 0 oddP → starts at index 1 Whenever we find an even number → place it at evenP, move evenP += 2 Whenever we find an odd number → place it at oddP, move oddP += 2 🔷Approach : 1️⃣ Create a result array res of the same size 2️⃣ Track even and odd positions using two pointers 3️⃣ Traverse the array once 4️⃣ Place each number at the correct index (even or odd) 5️⃣ Return the result array Time Complexity: O(n) Space Complexity: O(n) This problem helps build intuition for index-based placement and parity logic. Place elements where they belong — efficient, simple, and clean ✅ This pattern is useful in : ✅ Rearrangement problems ✅ Partitioning by conditions ✅ Two-pointer placement algorithms Every new problem improves how I think about arrays and patterns. Step-by-step progress, every single day 🔥 #Day118 #DSA #100DaysOfCode #LeetCode #Java #Arrays #TwoPointers #ProblemSolving #CodingJourney #Algorithms #DataStructures #EngineerMindset #GrowEveryday
To view or add a comment, sign in
-
-
🔥 Day 113 of My DSA Challenge – Word Search 🔷 Problem : 79. Word Search 🔷 Goal : Check if a given word exists in a 2D grid by connecting sequentially adjacent letters (horizontally or vertically). Same cell cannot be used more than once. 🔷 Key Insight : This is a DFS + backtracking problem. Start from every cell matching the first character. Explore all 4 directions recursively. Track visited cells to avoid reusing letters. Backtrack when a path fails. This teaches how to explore grids efficiently using recursion and state tracking. 🔷 Approach : 1️⃣ Iterate over all cells to find starting points 2️⃣ DFS recursively for adjacent letters 3️⃣ Use a visited matrix to avoid reusing cells 4️⃣ Backtrack if the path does not lead to the word Time Complexity: O(m × n × 4^k) where k = length of word Space Complexity: O(m × n) for visited + recursion stack Word Search reinforces grid DFS + backtracking patterns. Explore all possibilities, track state carefully, backtrack on failure. This pattern is useful for : ✅ Maze solving ✅ Island counting problems ✅ Pathfinding & puzzle problems Every day, a new pattern becomes familiar. Step by step, the mind gets sharper. 🚀 #Day113 #DSA #100DaysOfCode #DFS #Backtracking #LeetCode #ProblemSolving #Java #CodingChallenge #Algorithms #DataStructures #DeveloperJourney
To view or add a comment, sign in
-
-
🔥 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 114 of My DSA Challenge – Intersection of Two Arrays 🔷 Problem : 349. Intersection of Two Arrays 🔷 Goal : Find the unique common elements between two integer arrays. The order of elements doesn’t matter. 🔷 Key Insight : This is a Hashing-based problem. We need to identify the numbers that appear in both arrays — but each element should appear only once in the result. The idea is simple : 1️⃣ Store all elements of the first array in a HashMap (or HashSet). 2️⃣ Traverse the second array and check for matches. 3️⃣ Whenever a match is found → add it to the result and remove it to avoid duplicates. 🔷 Approach : 1️⃣ Use a HashMap to store unique elements from nums1. 2️⃣ Iterate through nums2, and if an element exists in the map → add it to the result list. 3️⃣ Remove the element from the map after adding (to prevent duplicates). 4️⃣ Convert the result list to an array and return it. Time Complexity: O(n + m) Space Complexity: O(n) 🔷 Takeaway : This problem strengthens hash-based searching and uniqueness handling — Use HashMap / HashSet when you need fast lookups and uniqueness control. ✅ This logic is frequently used in : Duplicate removal problems Union & intersection operations Set theory-based DSA problems ✨ Another simple yet powerful concept mastered. Every small step counts toward building a strong foundation. 💪 #Day114 #DSA #100DaysOfCode #HashMap #HashSet #LeetCode #ProblemSolving #Java #CodingChallenge #Algorithms #DataStructures #DeveloperJourney #LogicBuilding #EngineerMindset
To view or add a comment, sign in
-
-
🚀 Day 42 of #100DaysOfCode – Striver’s DSA Sheet 🚀 ✅ Problem Covered Today: Palindrome Number 🧩 (LeetCode #9) 💡 Lesson of the Day (Approach-Focused): 🔹 Problem Statement: Given an integer x, return true if x is a palindrome, and false otherwise. A palindrome number reads the same forward and backward. 🧩 Example: Input → x = 121 Output → true Explanation → 121 reads the same both ways. 🧠 Approach: Mathematical Reversal (Without String Conversion) 1️⃣ If x is negative → directly return false. 2️⃣ Store the original number in a variable. 3️⃣ Reverse the number digit by digit using modulus (% 10) and division (/ 10). 4️⃣ Compare the reversed number with the original. If equal → it’s a palindrome. Otherwise → not a palindrome. 🧮 Time Complexity: O(log₁₀n) → number of digits 💾 Space Complexity: O(1) ✨ Key Idea: By using pure arithmetic operations, we avoid extra memory and string manipulation, making the solution efficient and elegant. It’s a great warm-up problem for digit-based logic and reverse-integer type challenges. 💭 Learning: Today’s problem strengthened my control over mathematical operations and logical flow — helping me think beyond string-based shortcuts. #100DaysOfCode #DSA #StriversSheet #LeetCode #PalindromeNumber #ProblemSolving #Java #CodingJourney #LogicBuilding #Consistency #Learning
To view or add a comment, sign in
-
-
💡 Problem-Solving Post: Surrounded Regions in a Grid Today I worked on an interesting problem that really tested my understanding of graph traversal and boundary conditions. 🧩 Problem Summary: Given a 2D grid containing 'O's and 'X's, the goal is to replace all 'O's that are completely surrounded by 'X's. In other words, any 'O' that is not connected to the border should be converted into 'X'. ⚙️ Key Idea: At first glance, it looks like a simple replacement problem — but it’s not! The challenge is to differentiate between ‘O’s that are truly surrounded and those that are connected to the border. 🧠 Approach: Identify all 'O's on the borders (they can never be surrounded). From each border 'O', perform a DFS/BFS traversal to mark all connected 'O's as “safe.” After marking, flip the remaining 'O's (which are not safe) into 'X'. 🚀 What I Learned: The importance of thinking beyond the direct condition — sometimes the solution lies in what’s connected to the edge. How depth-first or breadth-first traversal can simplify problems that initially seem complex. A reminder that clean logic and clear reasoning often matter more than complex code. 👨💻 Complexity: Time: O(n × m) Space: O(n × m) (due to recursion or queue storage) It’s a simple yet elegant application of graph traversal that blends problem-solving and optimization thinking. Have you solved a similar boundary-based traversal problem recently? Would love to hear your approach or variations! #ProblemSolving #DSA #GraphAlgorithms #CodingJourney #LearningEveryday #Java
To view or add a comment, sign in
-
-
🔥 Day 121 of My DSA Challenge – Remove Duplicates from Sorted List 🔷 Problem : 83. Remove Duplicates from Sorted List 🔷 Goal : Given the head of a sorted linked list, remove all duplicate nodes so that each element appears only once, while maintaining the sorted order. 🔷 Key Insight : This problem is a great example of linked list traversal and pointer management. Because the list is already sorted, all duplicates appear consecutively — which makes it easy to detect and skip them in one pass. The challenge is to handle links carefully so that only unique nodes remain connected. 🔷 Approach : 1️⃣ Initialize a dummy node (with a distinct value) to simplify linking. 2️⃣ Use a tail pointer to build a new list containing only unique elements. 3️⃣ Traverse the list using head: If head.val is not equal to the last added node (tail.val), link it. Otherwise, skip the duplicate. 4️⃣ Update tail.next = null at the end to avoid leftover links. 5️⃣ Return dummy.next as the new head. Time Complexity: O(n) Space Complexity: O(1) This problem strengthens concepts of : ✅ Duplicate handling in linked lists ✅ Pointer-based traversal ✅ Clean in-place modification Sometimes, cleaning a data structure is just about connecting only what truly matters. ⚡ Every small pattern makes a big difference in problem-solving clarity. One more concept locked in 💻 #Day121 #DSA #100DaysOfCode #LeetCode #Java #LinkedList #ProblemSolving #Algorithms #DataStructures #CodingChallenge #DeveloperJourney #EngineerMindset #GrowEveryday
To view or add a comment, sign in
-
-
🔥 Day 120 of My DSA Challenge – Remove Linked List Elements 🔷 Problem : 203. Remove Linked List Elements 🔷 Goal : Remove all nodes from a linked list whose value equals a given val, and return the updated head. 🔷 Key Insight : This problem focuses on Linked List traversal and pointer manipulation, one of the most fundamental concepts in data structures. Directly removing nodes from a linked list can get tricky, especially when dealing with the head node itself. To simplify this, we use a dummy node technique — a smart approach to handle head removals gracefully. 🔷 Approach : 1️⃣ Create a dummy node (dummy) that points to the start of the list. 2️⃣ Use a pointer (tail) to build a new filtered list. 3️⃣ Traverse the original list using head: If head.val != val, link it to the new list. Otherwise, skip the node. 4️⃣ After traversal, ensure tail.next = null to avoid cycles. 5️⃣ Return dummy.next (new head). Time Complexity: O(n) Space Complexity: O(1) This problem reinforces : ✅ Pointer management ✅ Dummy node usage ✅ Clean and safe linked list manipulation When handling linked lists, think in pointers — not positions. Efficient traversal and clean linking make all the difference. ⚡ 120 days strong. One more concept mastered, one step closer to mastery. 🚀 #Day120 #DSA #100DaysOfCode #LeetCode #Java #LinkedList #Pointers #ProblemSolving #Algorithms #DataStructures #CodingChallenge #DeveloperJourney #EngineerMindset #LogicBuilding
To view or add a comment, sign in
-
-
🔥 Day 222 - Daily DSA Challenge! 🔥 Problem: 🌳 Flatten Binary Tree to Linked List Given the root of a binary tree, flatten it into a linked list in-place, following the preorder traversal order (root → left → right). 💡 Key Insights: 🔹 The goal is to modify the tree without using extra space. 🔹 For each node, if a left subtree exists: 1️⃣ Find the rightmost node of the left subtree. 2️⃣ Connect that node’s right pointer to the current node’s right subtree. 3️⃣ Move the left subtree to the right and set left = null. 🔹 Repeat this process while traversing through the tree. ⚡ Optimized Plan: ✅ Traverse using a pointer (curr). ✅ If curr.left exists, attach it in place of curr.right and re-link the subtrees. ✅ Continue the process until the end of the tree. ✅ Time Complexity: O(n) — each node visited once. ✅ Space Complexity: O(1) — in-place transformation. 💬 Challenge for you: 1️⃣ Can you flatten the tree using recursion instead of iteration? 2️⃣ How would you modify this to produce a postorder or inorder flattened list? #DSA #BinaryTree #LinkedList #LeetCode #ProblemSolving #CodingChallenge #KeepCoding #100DaysOfCode
To view or add a comment, sign in
-
-
𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐃𝐚𝐢𝐥𝐲 𝐂𝐡𝐚𝐥𝐥𝐞𝐧𝐠𝐞: 1513. 𝐍𝐮𝐦𝐛𝐞𝐫 𝐨𝐟 𝐒𝐮𝐛𝐬𝐭𝐫𝐢𝐧𝐠𝐬 𝐖𝐢𝐭𝐡 𝐎𝐧𝐥𝐲 1𝐬 Today’s problem was a clean exercise in mathematical pattern recognition within strings — proving that sometimes, counting smartly beats looping endlessly. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐮𝐦𝐦𝐚𝐫𝐲: We are given a binary string and need to count the number of substrings consisting entirely of '1's. Since the count can be large, the result is returned modulo 10⁹ + 7. 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: The core insight is recognizing how consecutive '1's form substrings: For a group of k consecutive '1's, the number of valid substrings = k * (k + 1) / 2. Instead of explicitly using this formula, I used a simple iterative approach: Keep a running counter for consecutive '1's. Add that counter to the result whenever we encounter another '1'. Reset the counter on a '0'. This yields an elegant O(n) single-pass solution with constant space. 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 𝐀𝐧𝐚𝐥𝐲𝐬𝐢𝐬: Time Complexity: O(n) Space Complexity: O(1) 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬: Mathematical series reasoning helps simplify substring problems. Consecutive counting avoids redundant substring generation. Simplicity and pattern recognition often lead to the most optimal logic. A great reminder that not every problem needs complexity — sometimes, it’s all about spotting the sequence. #LeetCode #DSA #Java #ProblemSolving #Algorithms #CodingPractice #LearningJourney #100DaysOfCode #Consistency #BinaryStrings
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
Two pointer pattern. Mostly asked in the interview! Well written and well articulated