BackendML

Move Zeroes

Easy3 min read
The Move Zeroes problem requires moving all zeroes to the end of an array while maintaining the relative order of the non-zero elements.

Problem

Given an integer array nums, move all 0s to the end of the array while maintaining the relative order of the non-zero elements.

The operation must be performed in-place without making a copy of the array.

Example(s)

Consider the following example(s) to understand the expected input and output.

Example 1

Input
nums = [0,1,0,3,12]
Output
[1,3,12,0,0]

Example 2

Input
nums = [0,0,1]
Output
[1,0,0]

Solution

There are multiple ways to solve this problem using the Two Pointer technique. The first approach uses a write pointer to place non-zero elements at the beginning of the array. The second approach uses two pointers from both ends and rotates the zero found on the left to the right.

Approach 1: Write Pointer

This solution uses a Two Pointer approach. The i pointer scans the entire array, while the write pointer keeps track of the next position where a non-zero element should be placed.

Whenever nums[i] is non-zero, we copy it to nums[write] and increment write. This moves all non-zero elements to the beginning of the array while preserving their relative order.

After all non-zero elements have been processed, write points to the first position that should contain a zero. We then fill all remaining positions with 0.

This approach modifies the array in-place without requiring an additional array.
class Solution {
    public void moveZeroes(int[] nums) {
        int write = 0;
        int i = 0;
        int n = nums.length;

        while (i < n) {
            if (nums[i] != 0) {
                nums[write++] = nums[i];
            }
            i++;
        }

        for (i = write; i < n; i++) {
            nums[i] = 0;
        }
    }
}

Approach 2: Two Pointers with Rotation

This approach also uses Two Pointers, but instead of maintaining a separate write pointer, it uses two pointers, i and j, starting from the beginning and end of the array.

The i pointer searches for the first zero from the left, while the j pointer searches for the first non-zero element from the right.

Once both positions are found, we rotate the elements from i to j by one position. This moves the zero at i toward the end while preserving the relative order of the non-zero elements.

The process continues until the two pointers meet.
class Solution {
    public void moveZeroes(int[] nums) {
        int i = 0;
        int j = nums.length - 1;

        while (i < j) {
            while (i < j && nums[i] != 0) {
                i++;
            }

            while (i < j && nums[j] == 0) {
                j--;
            }

            rotateByOne(nums, i, j);
        }
    }

    private void rotateByOne(int[] nums, int i, int j) {
        int tmp = nums[i];

        for (int k = i + 1; k <= j; k++) {
            nums[k - 1] = nums[k];
        }

        nums[j] = tmp;
    }
}

Complexity

For the Write Pointer approach, the array is traversed once to move the non-zero elements and once to fill the remaining positions with zeroes. Therefore, the time complexity is O(n).

The solution modifies the input array directly and uses only a few variables, so the extra space complexity is O(1).

For the Rotation approach, finding the two positions takes linear traversal overall, but rotateByOne() can shift multiple elements for every zero that needs to be moved. In the worst case, this results in O(n²) time complexity.

Since the rotation is performed directly on the input array and only a few variables are used, the extra space complexity is O(1).

Comments

No comments yet. Ask a question or share what helped.