# Move Zeros to End in Java

An array of unsorted integers is given. Our task is to move all the zero elements to the end and non-zero elements to the front. Note the relative arrangement of the non-zero elements should never get disturbed. The following examples make things clearer.

Example 1:

Input: int arr[] = {6, 7, 0, 2, 1, 78, 0, 56, 0, 4}

Output: arr[] = {6, 7, 2, 1, 78, 56, 4, 0, 0, 0}

Explanation: While maintaining the relative order of the non-zero elements, it is the only solution possible. Note that the relative order means if the position of one element is before the other element, then that order has to be maintained in the output. In the input array, 78 comes before 56; therefore, in the output also, 78 comes before 56. The same logic can be applied to the other non-zero elements of the array.

Example 2:

Input: int arr[] = {8, 9, 2, 3, 0, 0, 0}

Output: arr[] = {8, 9, 2, 3, 0, 0, 0}

Explanation: Here, in the input array, the zeros are already there at the end. Hence, without any modification, we can return the input array.

## Approach: Using Auxiliary Array

The approach is simple. Observe the following steps.

Step 1: Create an auxiliary array of the same size as the input array.

Step 2: Create a variable zeroCount, and initialize it with zero. Also, create a variable outputIndex, initializing with the value 0.

Step 3: Using a loop, iterate over the elements of the input array. If an element with the zero value is encountered, increment the zeroCount by 1. If an element with a non-zero value is encountered, copy the value of that element in the element auxiliarArray[outputIndex], and increment the value of outputIndex by 1.

Step 4: Add 0 to the end of the auxiliary array. The number of zero should be equal to the value contained in the variable zeroCount. It can be done with the help of loop.

Step 5: Display the output array on the console.

### Implementation

Observe the implementation of the mentioned steps.

FileName: MoveZeros.java

Output:

```For the following array:
6 7 0 2 1 78 0 56 0 4
The answer after moving the zero at the end is:
6 7 2 1 78 56 4 0 0 0

For the following array:
8 9 2 3 0 0 0
The answer after moving the zero at the end is:
8 9 2 3 0 0 0
```

Complexity Analysis: Since the loop is used for iterating over the element and appending zero at the end in the method solve(), the time complexity of the program is O(N). Also, an auxiliary array is used for storing the result. Therefore, the space complexity of the program is O(N), where N is the total number of elements present in the input array.

We will do the optimization in terms of the space complexity in the following approach.

## Approach: Using Two Pointers

We will take two-pointers. One pointer of the zero-valued elements and the other for the non-zero valued elements. The task is to put zero-valued elements towards the end. Therefore, the non-zero pointer points only when a non-zero element is encountered after the zero-element pointer. Whenever we see that the non-zero and the zero pointers are set, the elements can be swapped that are located at these places in the input array. After that, we move the zero-pointer further. We also move the non-zero pointer until a non-zero element is found. When the non-zero pointer reaches the end of the input array, we stop. The illustration of the same is mentioned below.

### Implementation

FileName: MoveZeros1.java

Output:

```For the following array:
6 7 0 2 1 78 0 56 0 4
The answer after moving the zero at the end is:
6 7 2 1 78 56 4 0 0 0

For the following array:
8 9 2 3 0 0 0
The answer after moving the zero at the end is:
8 9 2 3 0 0 0
```

Complexity Analysis

The time complexity of the program is the same as the previous program. Since no auxiliary space is used in the program, the space complexity of the program is constant, i.e., O(1).