Given an array arr[] of N positive integers. Push all the zeros 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}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3



class Solution:

	def pushZerosToEnd(self,arr, n):

    	# code here

Solutions:
class Solution:

	def pushZerosToEnd(self, arr, n):
		a = 0
		for i in range(n):
			if arr[i] != 0:
				arr[a] = arr[i]
				a += 1
		for j in range(a, n):
			arr[j] = 0


class Solution:

	def pushZerosToEnd(self, arr, n):
		j = 0
		for i in range(n):
			if arr[j] == 0 and arr[i] != 0:
				(arr[j], arr[i]) = (arr[i], arr[j])
				j += 1
			elif arr[j] != 0:
				j += 1


class Solution:

	def pushZerosToEnd(self, arr, n):
		temp = []
		for i in range(n):
			if arr[i] != 0:
				temp.append(arr[i])
		for i in range(n):
			if i < len(temp):
				arr[i] = temp[i]
			else:
				arr[i] = 0


class Solution:

	def pushZerosToEnd(self, nums, n):
		i = -1
		for x in range(n):
			if arr[x] == 0:
				i = x
				break
		while i >= 0:
			j = i + 1
			while j < n:
				if nums[j] != 0:
					(nums[i], nums[j]) = (nums[j], nums[i])
					i += 1
				j += 1
			return nums
		return nums


class Solution:

	def pushZerosToEnd(self, arr, n):
		i = 0
		j = 0
		while arr[i] == 0:
			break
			i += 1
		for k in range(n):
			if arr[k] != 0:
				(arr[k], arr[i]) = (arr[i], arr[k])
				i += 1


class Solution:

	def pushZerosToEnd(self, A, n):
		j = 0
		for i in range(n):
			if A[i] != 0:
				(A[j], A[i]) = (A[i], A[j])
				j += 1


class Solution:

	def pushZerosToEnd(self, arr, n):
		j = -1
		for i in range(len(arr)):
			if arr[i] == 0:
				j = i
				break
		if j == -1:
			return arr
		for i in range(j + 1, len(arr)):
			if arr[i] != 0:
				(arr[i], arr[j]) = (arr[j], arr[i])
				j = j + 1
		return arr


class Solution:

	def pushZerosToEnd(self, arr, n):
		c = 0
		j = 0
		for i in arr:
			if i != 0:
				arr[j] = i
				j += 1
		d = n - j
		for i in range(d):
			arr[j] = 0
			j += 1
		return arr


class Solution:

	def pushZerosToEnd(self, arr, n):
		slow = 0
		for fast in range(n):
			if arr[fast] != 0 and arr[slow] == 0:
				(arr[slow], arr[fast]) = (arr[fast], arr[slow])
			if arr[slow] != 0:
				slow += 1


class Solution:

	def pushZerosToEnd(self, arr, n):
		a = []
		b = []
		for i in arr:
			if i == 0:
				a.append(i)
			else:
				b.append(i)
		arr[:] = b + a
		return arr


class Solution:

	def pushZerosToEnd(self, arr, n):
		d = []
		e = []
		for i in arr:
			if i == 0:
				d.append(i)
			else:
				e.append(i)
		arr[:] = e + d
		return arr


class Solution:

	def pushZerosToEnd(self, arr, n):
		p1 = 0
		count = 0
		for i in range(n):
			if arr[i] == 0:
				p1 = i
				break
			else:
				count += 1
		if count != n:
			p2 = p1 + 1
			while p2 < n:
				if arr[p2] != 0:
					(arr[p1], arr[p2]) = (arr[p2], arr[p1])
					p1 += 1
					p2 += 1
				else:
					p2 += 1


class Solution:

	def pushZerosToEnd(self, arr, n):
		arr[:] = [i for i in arr if i != 0] + [0] * arr.count(0)


class Solution:

	def pushZerosToEnd(self, arr, n):
		nzero = []
		zero = []
		for i in range(n):
			if arr[i] == 0:
				zero.append(arr[i])
			else:
				nzero.append(arr[i])
		arr[:] = nzero + zero
		return arr


class Solution:

	def pushZerosToEnd(self, arr, n):
		nzero = []
		zero = []
		dict = []
		n = len(arr)
		for i in range(n):
			if arr[i] == 0:
				zero.append(arr[i])
			elif arr[i] != 0:
				nzero.append(arr[i])
		for i in nzero:
			dict.append(i)
		for i in zero:
			dict.append(i)
		arr[:] = dict[:]
		return arr


class Solution:

	def pushZerosToEnd(self, arr, n):
		a = arr.count(0)
		b = []
		for i in arr:
			if i != 0:
				b.append(i)
		while a > 0:
			b.append(0)
			a -= 1
		arr.clear()
		arr.extend(b)
		return arr


class Solution:

	def pushZerosToEnd(self, arr, n):
		nextNonZeroPos = 0
		for i in range(n):
			if arr[i] != 0:
				arr[nextNonZeroPos] = arr[i]
				nextNonZeroPos += 1
		for i in range(nextNonZeroPos, n):
			arr[i] = 0
		return arr


class Solution:

	def pushZerosToEnd(self, arr, n):
		j = 0
		s = 0
		for i in range(n):
			if arr[i] != 0:
				arr[j] = arr[i]
				j += 1
		while j < n:
			arr[j] = 0
			j += 1


class Solution:

	def pushZerosToEnd(self, arr, n):
		count = 0
		for i in range(n):
			if arr[i] != 0:
				arr[count] = arr[i]
				count += 1
		while count < n:
			arr[count] = 0
			count += 1


class Solution:

	def pushZerosToEnd(self, arr, n):
		j = -1
		for i in range(n):
			if arr[i] == 0:
				j = i
				break
		if j != -1:
			for i in range(j + 1, n):
				if arr[i] != 0:
					temp = arr[j]
					arr[j] = arr[i]
					arr[i] = temp
					j += 1


class Solution:

	def pushZerosToEnd(self, arr, n):
		l = []
		lst = []
		for i in arr:
			if i == 0:
				l.append(i)
			else:
				lst.append(i)
		arr[:] = lst + l


class Solution:

	def pushZerosToEnd(self, arr, n):
		ans = [x for x in arr if x > 0]
		l = len(ans)
		i = 0
		for x in ans:
			arr[i] = x
			i += 1
		for x in range(i, n):
			arr[i] = 0
			i += 1
		return arr


class Solution:

	def pushZerosToEnd(self, arr, n):
		zeros = []
		nonzeros = []
		result = []
		for i in range(n):
			if arr[i] == 0:
				zeros.append(arr[i])
			else:
				nonzeros.append(arr[i])
		for i in range(len(nonzeros)):
			result.append(nonzeros[i])
		for i in range(len(zeros)):
			result.append(zeros[i])
		for i in range(len(result)):
			arr[i] = result[i]
		return arr


class Solution:

	def pushZerosToEnd(self, arr, n):
		count = 0
		for i in range(n):
			if arr[i] != 0:
				temp = arr[i]
				arr[i] = arr[count]
				arr[count] = temp
				count += 1
		return


class Solution:

	def pushZerosToEnd(self, arr, n):
		ind = 0
		for i in range(n):
			if arr[i] != 0:
				(arr[ind], arr[i]) = (arr[i], arr[ind])
				ind += 1


class Solution:

	def pushZerosToEnd(self, arr, n):
		new_arr = []
		for i in range(0, n):
			if arr[i] != 0:
				new_arr.append(arr[i])
		while len(new_arr) < n:
			new_arr.append(0)
		arr[:] = new_arr


class Solution:

	def pushZerosToEnd(self, arr, n):
		i = 0
		c = 0
		while i < n:
			if arr[i] == 0:
				del arr[i]
				c += 1
			else:
				i += 1
			if i >= len(arr):
				break
		arr.extend([0] * c)


class Solution:

	def pushZerosToEnd(self, arr, n):
		s = 0
		for i in range(0, n):
			if arr[i] != 0 and arr[s] == 0:
				(arr[i], arr[s]) = (arr[s], arr[i])
			if arr[s] != 0:
				s += 1
		return arr


class Solution:

	def pushZerosToEnd(self, arr, n):
		a = 0
		for i in range(len(arr)):
			if arr[i] != 0:
				(arr[a], arr[i]) = (arr[i], arr[a])
				a += 1
		return arr


class Solution:

	def pushZerosToEnd(self, arr, n):
		i = 0
		j = 1
		while j < n:
			if arr[i] == 0 and arr[j] != 0:
				(arr[i], arr[j]) = (arr[j], arr[i])
				i += 1
			elif arr[i] != 0:
				i += 1
			j += 1
		return arr


class Solution:

	def pushZerosToEnd(self, arr, n):
		index = 0
		for i in range(n):
			if arr[i] != 0:
				arr[index] = arr[i]
				index += 1
		while index < n:
			arr[index] = 0
			index += 1
		return arr


class Solution:

	def pushZerosToEnd(self, arr, n):
		a = 0
		b = 1
		while a < n and b < n:
			if arr[a] == 0 and arr[b] == 0:
				b += 1
			elif arr[a] == 0 and arr[b] != 0:
				(arr[a], arr[b]) = (arr[b], arr[a])
				a += 1
				b += 1
			else:
				a += 1
				b += 1


class Solution:

	def pushZerosToEnd(self, arr, n):
		i = 0
		while i < n:
			if arr[i] == 0:
				break
			i += 1
		j = i
		k = i + 1
		while j < n and k < n:
			if arr[k] != 0:
				(arr[j], arr[k]) = (arr[k], arr[j])
				j += 1
			k += 1
		return arr


class Solution:

	def pushZerosToEnd(self, arr, n):
		i = 0
		j = 0
		while j < n:
			if arr[j] == 0:
				j += 1
				break
			else:
				j += 1
		while j < n:
			if arr[i] == 0:
				while True:
					if arr[j] == 0:
						j += 1
					else:
						break
					if j == n:
						return
				(arr[i], arr[j]) = (arr[j], arr[i])
				j += 1
			else:
				i += 1
			if i == n:
				break


class Solution:

	def pushZerosToEnd(self, arr, n):
		i = 0
		c = 0
		while i <= len(arr) - 1:
			if arr[i] == 0:
				arr.pop(i)
				c = c + 1
			else:
				i = i + 1
		for i in range(0, c):
			arr.append(0)
		return arr


class Solution:

	def pushZerosToEnd(self, arr, n):
		c = 0
		i = 0
		while i <= n - 1:
			if arr[i] != 0:
				(arr[i], arr[c]) = (arr[c], arr[i])
				c += 1
			i = i + 1
		return arr


class Solution:

	def pushZerosToEnd(self, arr, n):
		l = []
		s = 0
		for i in arr:
			if i != 0:
				l.append(i)
			else:
				s += 1
		l.extend([0] * s)
		for i in range(n):
			arr[i] = l[i]


class Solution:

	def pushZerosToEnd(self, a, n):
		i = 0
		c = 0
		while i < n:
			if a[i] != 0 and c != 0:
				a[i - c] = a[i]
			elif a[i] == 0:
				c += 1
			i += 1
		for j in range(n - c, n):
			a[j] = 0


class Solution:

	def pushZerosToEnd(self, arr, n):
		(l, r) = (0, 1)
		while r < n:
			if arr[l] != 0 and arr[r] == 0:
				l += 1
				r += 1
			elif arr[l] == 0 and arr[r] != 0:
				temp = arr[l]
				arr[l] = arr[r]
				arr[r] = temp
				l += 1
				r += 1
			elif arr[l] == 0 and arr[r] == 0:
				r += 1
			else:
				l += 1
				r += 1


class Solution:

	def pushZerosToEnd(self, arr, n):
		l = [a for a in arr if a != 0]
		m = len(l)
		if n == m:
			for i in range(n):
				arr[i] = l[i]
			l = 0
		else:
			z = [0 for i in range(n - m)]
			l.extend(z)
			for i in range(n):
				arr[i] = l[i]
			l = 0


class Solution:

	def pushZerosToEnd(self, arr, n):
		l1 = []
		l2 = []
		for i in arr:
			if i != 0:
				l1.append(i)
			else:
				l2.append(i)
		l3 = l1 + l2
		arr[:] = l3[:]
		return arr


class Solution:

	def pushZerosToEnd(self, arr, n):
		n = len(arr)
		l = 0
		for r in range(n):
			if arr[l] == 0 and arr[r] != 0:
				arr[l] = arr[r]
				arr[r] = 0
				l += 1
			elif arr[l] != 0:
				l += 1
				r += 1
		return arr


class Solution:

	def pushZerosToEnd(self, arr, n):
		zero_index = 0
		while zero_index < n:
			if arr[zero_index] == 0:
				break
			zero_index += 1
		(i, j) = (zero_index, zero_index + 1)
		while j < n:
			if arr[j] != 0:
				(arr[i], arr[j]) = (arr[j], arr[i])
				i += 1
			j += 1


class Solution:

	def pushZerosToEnd(self, arr, n):
		lIndex = 0
		RIndex = 0
		while RIndex < len(arr):
			if arr[RIndex] != 0:
				(arr[lIndex], arr[RIndex]) = (arr[RIndex], arr[lIndex])
				lIndex += 1
			RIndex += 1
		return arr


class Solution:

	def pushZerosToEnd(self, arr, n):
		distance = n
		index = 0
		while index < n:
			if arr[index] == 0:
				arr.pop(index)
				n -= 1
			else:
				index += 1
		arr.extend([0] * (distance - n))