# Move all the zeros to the end of array in C++

In this problem, we will get an array of size ‘n’. we need to move all the zeros to the end of the array without changing the order of non-zero numbers.

Let’s take an example to understand it in a better way.

Example 1:

input : 1 0 2 5 0 0 7 9 4 0 5 0 output: 1 2 5 7 9 4 5 0 0 0 0 0 input : 0 0 1 11 12 45 0 0 14 5 4 2 0 0 4 output: 1 11 12 45 14 5 4 2 4 0 0 0 0 0 0 input : 1 9 0 4 2 0 4 5 6 4 7 output: 1 9 4 2 4 5 6 4 7 0 0

There are many approaches/methods to solve this problem but in this tutorial, we will understand two easy methods.

## First method: Move all the zeros to the end of array in C++

In method 1 first, we will traverse the array from left to right and count the non-zero elements. let the count of non-zero elements be **‘count**‘. If non-zero element found, put the element at **arr[count]**. And we will increment count also. By this, after traversing all the non-zero elements will shift to the leftmost side of the array in the original order. now we will have the end index number after non-zero elements from where we need to put the zeros. Therefore all we have to do is to start the loop from count to the size of the array and put the zeros for each **arr[count]**.

**Algorithm**

- Declare an integer type array and two variable for array size and to count the non-zero elements. (let a variable to count non-zero elements be
**nonZero**) - Input the number in the array or initialize the array.
- Start a loop form i = 0 to the size of the array, for every
**arr[i] != 0**, put the element at**arr[nonZero]**and increment nonZero by**one**. - end the loop.
- start another loop from
**nonZero**to the**size**of the array. For every**arr[nonZero]**, put**zero**. - end the second loop.
- print the elements of the array.

### C++ program to Move all the zeros to the end of the array using method 1

#include<iostream> using namespace std; int main(){ int arr[50], n, nonZero = 0; cout<<"Enter the size of array: "; cin>>n; cout<<"Input the numbers: "; for(int i = 0; i < n ; i++) cin>>arr[i]; //moving all the non-zero elements at the left side for(int i = 0; i < n; i++){ if(arr[i] != 0){ arr[nonZero] = arr[i]; nonZero++; } } //putting all the zeros at the last of array for(int i = nonZero; i < n; i++) arr[i] = 0; //printing the modified array cout<<"\nArray after moving all the zero at the end: "; for(int i = 0; i < n; i++) cout<<arr[i]<<" "; return 0; }

**Output: **

Enter the size of array: 10 Input the numbers: 1 0 2 0 0 5 7 8 0 6 Array after moving all the zero at the end: 1 2 5 7 8 6 0 0 0 0

**Time Complexity: **O(n), where n is the size of the array.

**Second method: Move all the zeros to the end of array**

In this method first, we will traverse the array from left to right and count the non-zero elements.let the count of non-zero elements be **‘count**‘.For every non-zero element **a****rr[i]**a, we will swap the element with **arr[count]** and increment **‘count’** by one.

**Algorithm**

- Declare an integer type array and two variable for array size and to count the non-zero elements. (let a variable to count non-zero elements be
**nonZero**) - Input the number in the array or initialize the array.
- Start a loop form i = 0 to the size of the array, for every
**arr[i] != 0**, swap**arr[nonZero]**with**arr[i]**and increment nonZero by**one**. - end the loop.
- print the elements of the array.

## Method 2: C++ program to Move all the zeros to the end of the array

#include<iostream> using namespace std; int main(){ int arr[50], n, nonZero = 0; cout<<"Enter the size of array: "; cin>>n; cout<<"Input the numbers: "; for(int i = 0; i < n ; i++) cin>>arr[i]; //putting all the zeros at the end of array for(int i = 0; i < n; i++){ if(arr[i] != 0){ swap(arr[nonZero], arr[i]); nonZero++; } } cout<<"\nArray after moving all the zero at the end: "; for(int i = 0; i < n; i++) cout<<arr[i]<<" "; return 0; }

**output: **

Enter the size of array: 10 Input the numbers: 1 0 2 0 0 5 7 8 0 6 Array after moving all the zero at the end: 1 2 5 7 8 6 0 0 0 0

**Time complexity: **O(n), where n is the size of the array

**You may also read: **

- How to insert an element in an array in C++
- How to reverse a vector in c++
- Maximum subarray sum of a given array using c++

## Leave a Reply