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


๐๏ธ 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
Subscribe to my newsletter
Read articles from Tanishi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
