Geeksforgeeks

X

DAYS

:

HOUR

:

MINS

:

SEC

Error

Copied to Clipboard

Given an array **arr[]** of **N** positive integers. Push all the zero’s of the given array to the right end of the array while maitaining the order of non-zero elements.

**Example 1:**

**Input:
**N = 5
Arr[] = {3, 5, 0, 0, 4}
**Output:** 3 5 4 0 0
**Explanation:** The non-zero elements
preserve their order while the 0
elements are moved to the right.

**Example 2:**

**Input:
**N = 4
Arr[] = {0, 0, 0, 4}
**Output:** 4 0 0 0
**Explanation:** 4 is the only non-zero
element and it gets moved to the left.

**Your Task:**

You don't need to read input or print anything. Complete the function **pushZerosToEnd()** which takes the array **arr[] **and its size **n** as input parameters and modifies arr[] in-place such that all the zeroes are moved to the right.

**Expected Time Complexity:** O(N)

**Expected Auxiliary Space:** O(1)

**Constraints:**

1 ≤ N ≤ 10^{5}

0 ≤ arr_{i} ≤ 10^{5}

We are replacing the old Disqus forum with the new Discussions section given below.

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Move all zeroes to end of array

...