Moving Zeroes to End with Two Pointer Technique

🚀 #100DaysOfCode – Day 16 | DSA Practice Continuing my 100 Days Data Structures and Algorithms challenge, today I solved a popular problem based on arrays and two-pointer technique. 📌 Problem: Move Zeroes Given an integer array nums, move all 0’s to the end while maintaining the relative order of non-zero elements. The operation must be done in-place. Example: Input: nums = [0,1,0,3,12] Output → [1,3,12,0,0] 🧠 Approach / Logic: 1️⃣ Use two pointers: • i → to traverse the array • j → to track the position for non-zero elements 2️⃣ Traverse the array from left to right. 3️⃣ If the current element is non-zero, swap it with the element at index j. 4️⃣ Increment j to point to the next position. 5️⃣ Continue this process for the entire array. 6️⃣ At the end, all non-zero elements will be at the front, and all zeros will be shifted to the end. 📊 Time Complexity: O(n) 📦 Space Complexity: O(1) 🎯 Key Learning: This problem demonstrates how the two-pointer technique can efficiently solve in-place array problems while maintaining order. Consistency is the key to growth. Let’s keep improving every day! 💪 #100DaysOfCode #DSA #CodingJourney #ProblemSolving #CPP #LearningInPublic

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories