Day 1 โ€“ Move Zeroes | Two Pointer Approach | LeetCode Challenge

TanishiTanishi
1 min read

๐Ÿ—“๏ธ Date: July 25, 2025


๐Ÿ“Œ Challenge:

๐Ÿงฉ Problem: Move Zeroes โ€“ LeetCode #283 (Easy)
๐Ÿ’ป Topic: Arrays, Two Pointers


โœ… Problem Statement (Summary):

Move all zeroes in an array to the end while maintaining the relative order of the non-zero elements.
Example:
Input: [0,1,0,3,12] โ†’ Output: [1,3,12,0,0]
Constraints: Must do it in-place with minimum operations.


๐Ÿง  What I Learned Today:

  • The Two Pointer approach is perfect when reordering in the same array.

  • Learned how to use one pointer for traversal and one to track non-zero insert positions.

  • Optimized in-place swaps instead of using extra space.


๐Ÿงช Code (C++):

class Solution {
public:
    void moveZeroes(vector<int>& nums) {
        int  e = 0;
       for(int s=0;s<nums.size();s++) {
            if (nums[s] != 0) {
                swap(nums[s], nums[e]);
                e++;
            }
        }
    }
};

๐Ÿ•’ Time Complexity: O(n)
๐Ÿ“ฆ Space Complexity: O(1)


๐Ÿ“ธ LeetCode Submission Screenshot:


๐Ÿ“ˆ Progress Tracker:

  • โœ… Day: 1 / 30

  • ๐Ÿงฉ Total Problems Solved: 1

  • ๐Ÿงญ Focus Today: Two Pointers


๐Ÿ—‚๏ธ Tags:

#leetcode #30DaysChallenge #twopointers #dsa #c++ #codingjourney #array

0
Subscribe to my newsletter

Read articles from Tanishi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tanishi
Tanishi