Introduction

The Move Zeroes problem involves shifting all zero elements to the end of the array while maintaining the relative order of non-zero elements.

Given an array arr[] of size n, the task is to move all 0 s to the end without changing the order of the remaining elements.

This is a popular beginner-level array problem that helps in understanding array manipulation, Two Pointer Technique, and in-place operations.

Example
Input: arr[] = [0, 1, 0, 3, 12]Output: [1, 3, 12, 0, 0]
Explanation:
All non-zero elements are shifted to the front.
All zeroes are moved to the end.
Input: arr[] = [4, 0, 5, 0, 0, 2]
Output: [4, 5, 2, 0, 0, 0]
Explanation:
Relative order of non-zero elements remains unchanged.
All zeroes are placed at the end.

Constraints

1 <= n <= 10^5

-10^9 <= arr[i] <= 10^9

Approach 1 : Brute Force (Using Extra Array)

Explanation

The simplest way to solve this problem is:

  1. Store all non-zero elements in another array
  2. Count the number of zeroes
  3. Append zeroes at the end

This approach is easy to understand but uses extra space.

Steps

  1. Create an empty array.
  2. Traverse the original array.
  3. Store all non-zero elements.
  4. Count total zeroes.
  5. Append all zeroes at the end.
  6. Return the final array.

Dry Run

Input Array: [0, 1, 0, 3, 12]Traverse 0:
Zero found
Count zeroes
Traverse 1:
Non-zero element
Store 1
Traverse 0:
Zero found
Count zeroes
Traverse 3:
Store 3
Traverse 12:
Store 12
Append two zeroes at the end
Final Result:
[1, 3, 12, 0, 0]

Brute Force Code

Complexity Analysis

Time Complexity: O(n)Explanation:
The array is traversed once.

Space Complexity: O(n)
Explanation:
An extra array is used to store non-zero elements.


    Approach 2 : Optimized Solution (Two Pointer Technique)

    Explanation

    Instead of using another array, we can solve this problem in-place using the Two Pointer Technique.

    The idea is:

    1. Maintain one pointer for placing non-zero elements
    2. Traverse the array
    3. Whenever a non-zero element is found:
      • swap it with the current position

    This keeps all non-zero elements at the front and pushes zeroes to the end automatically.

    Steps

    1. Initialize pointer j=0.
    2. Traverse the array using index i.
    3. If current element is non-zero:
      • swap arr[i] and arr[j]
      • increment j.
    4. Continue traversal.
    5. Final array will contain all zeroes at the end.

    Dry Run

    Input Array: [0, 1, 0, 3, 12]Initially:
    j = 0
    Traverse 0:
    Element is zero
    No swap required
    Traverse 1:
    Non-zero element found
    Swap arr[1] and arr[0]
    Array becomes:
    [1, 0, 0, 3, 12]
    Increment j = 1
    Traverse 0:
    Element is zero
    No swap required
    Traverse 3:
    Swap arr[3] and arr[1]
    Array becomes:
    [1, 3, 0, 0, 12]
    Increment j = 2
    Traverse 12:
    Swap arr[4] and arr[2]
    Array becomes:
    [1, 3, 12, 0, 0]
    Final Result:
    [1, 3, 12, 0, 0]

    Optimized Code 

Complexity Analysis

Time Complexity: O(n)Explanation:
Every element is visited exactly once.

Space Complexity: O(1)
Explanation: No extra array is used.
Only pointer variables are required.

Edge Cases

  1. Array contains all zeroes
  2. Array contains no zeroes
  3. Array contains only one element
  4. Zeroes are already at the end

Why This Problem is Important

This problem helps in understanding:

  1. Two Pointer Technique
  2. In-place array manipulation
  3. Swapping operations
  4. Traversal optimization
  5. Space optimization

It is one of the most important beginner-level array manipulation problems.

Real-World Applications

Move Zeroes logic is used in:

  1. Data filtering systems
  2. Sparse matrix processing
  3. Signal processing
  4. Memory optimization
  5. Data compression systems

Common Mistakes

  1. Incorrect swapping logic
  2. Forgetting pointer increment
  3. Changing relative order of elements
  4. Using unnecessary extra space

Interview Tips

Interviewers often expect:

  1. An in-place solution
  2. O(n) traversal
  3. Proper usage of Two Pointer Technique

Always explain why swapping non-zero elements is more space efficient.

Related Questions

  1. Remove Duplicates
  2. Rotate Array
  3. Rearrange Positive and Negative Numbers
  4. Sort Colors
  5. Move Negative Numbers

Final Takeaway

The Move Zeroes problem is a fundamental array manipulation problem that teaches in-place modification, swapping logic, and the Two Pointer Technique. Understanding this problem builds a strong foundation for advanced array optimization problems.