How to move all the zeros to the end of the array from the given array of integer numbers using C#?

Moving zeros to the end of an array is a common programming problem that can be solved efficiently using a two-pointer approach. The algorithm maintains the relative order of non-zero elements while shifting all zeros to the end of the array.

Algorithm Overview

The solution uses a single pass through the array with two pointers −

  • Write Pointer − tracks the position where the next non-zero element should be placed

  • Read Pointer − traverses through all elements in the array

Time Complexity − O(N) where N is the array length

Space Complexity − O(1) as we modify the array in-place

Moving Zeros Algorithm Original Array: 0 1 0 3 12 After Moving: 1 3 12 0 0 ? Zeros ? Non-zeros Non-zero elements maintain their relative order All zeros are moved to the end

Syntax

Following is the method signature for moving zeros to the end −

public void MoveZeros(int[] nums) {
    // Algorithm implementation
}

Implementation

Complete Example

using System;

public class Arrays {
    public void MoveZeros(int[] nums) {
        if (nums == null || nums.Length == 0) {
            return;
        }
        
        int writeIndex = 0;
        
        // Move all non-zero elements to the front
        for (int i = 0; i < nums.Length; i++) {
            if (nums[i] != 0) {
                nums[writeIndex] = nums[i];
                writeIndex++;
            }
        }
        
        // Fill remaining positions with zeros
        for (int i = writeIndex; i < nums.Length; i++) {
            nums[i] = 0;
        }
    }
    
    public void PrintArray(int[] nums) {
        Console.Write("[");
        for (int i = 0; i < nums.Length; i++) {
            Console.Write(nums[i]);
            if (i < nums.Length - 1) Console.Write(",");
        }
        Console.WriteLine("]");
    }
}

class Program {
    static void Main(string[] args) {
        Arrays solution = new Arrays();
        
        int[] nums1 = { 0, 1, 0, 3, 12 };
        Console.WriteLine("Original array:");
        solution.PrintArray(nums1);
        
        solution.MoveZeros(nums1);
        Console.WriteLine("After moving zeros:");
        solution.PrintArray(nums1);
        
        int[] nums2 = { 0, 0, 1 };
        Console.WriteLine("\nOriginal array:");
        solution.PrintArray(nums2);
        
        solution.MoveZeros(nums2);
        Console.WriteLine("After moving zeros:");
        solution.PrintArray(nums2);
    }
}

The output of the above code is −

Original array:
[0,1,0,3,12]
After moving zeros:
[1,3,12,0,0]

Original array:
[0,0,1]
After moving zeros:
[1,0,0]

How It Works

The algorithm works in two phases −

  1. First Pass − Copy all non-zero elements to the beginning of the array using a write pointer

  2. Second Pass − Fill the remaining positions with zeros

The writeIndex variable keeps track of where the next non-zero element should be placed. After processing all elements, writeIndex points to the first position where zeros should begin.

Alternative Approach with Swapping

Example

using System;

public class ArraysSwap {
    public void MoveZeros(int[] nums) {
        if (nums == null || nums.Length == 0) {
            return;
        }
        
        int left = 0;
        
        for (int right = 0; right < nums.Length; right++) {
            if (nums[right] != 0) {
                // Swap elements
                int temp = nums[left];
                nums[left] = nums[right];
                nums[right] = temp;
                left++;
            }
        }
    }
    
    public void PrintArray(int[] nums) {
        Console.Write("[");
        for (int i = 0; i < nums.Length; i++) {
            Console.Write(nums[i]);
            if (i < nums.Length - 1) Console.Write(",");
        }
        Console.WriteLine("]");
    }
}

class Program {
    static void Main(string[] args) {
        ArraysSwap solution = new ArraysSwap();
        
        int[] nums = { 0, 1, 0, 3, 12 };
        Console.WriteLine("Original array:");
        solution.PrintArray(nums);
        
        solution.MoveZeros(nums);
        Console.WriteLine("After moving zeros (swap approach):");
        solution.PrintArray(nums);
    }
}

The output of the above code is −

Original array:
[0,1,0,3,12]
After moving zeros (swap approach):
[1,3,12,0,0]

Conclusion

Moving zeros to the end of an array can be efficiently solved using a two-pointer approach with O(N) time complexity and O(1) space complexity. The algorithm preserves the relative order of non-zero elements while grouping all zeros at the end of the array.

Updated on: 2026-03-17T07:04:36+05:30

814 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements