Given an array
arr[]
of
n
elements filled with several integers, some of them being zeroes, write a program to
move all the zeroes to the end.
Problem Note
- The relative order of non-zero integers needs to remain the same as in the original array.
- You must do this in-place without making a copy of the array.
- Minimize the total number of operations.
Example 1
Input: arr[] = [1, 8, 0, 2, 0, 1, 13, 0]
Output: [1, 8, 2, 1, 13, 0, 0, 0]
Explanation: All the zeroes are moved to the end of array. The order of all other elements is kept same as it is in-place.
Example 2
Input: arr[] = [0, 0, 0, 23, 2]
Output: [23, 2, 0, 0, 0]
Explanation: All the zeroes are moved to the end of array. The order of all the other elements is kept same as it is in-place.