forked from rathoresrikant/HacktoberFestContribute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Move0sToEnd
34 lines (28 loc) · 913 Bytes
/
Move0sToEnd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Python3 code to move all zeroes
# at the end of array
# Function which pushes all
# zeros to end of an array.
def pushZerosToEnd(arr, n):
count = 0 # Count of non-zero elements
# Traverse the array. If element
# encountered is non-zero, then
# replace the element at index
# 'count' with this element
for i in range(n):
if arr[i] != 0:
# here count is incremented
arr[count] = arr[i]
count+=1
# Now all non-zero elements have been
# shifted to front and 'count' is set
# as index of first 0. Make all
# elements 0 from count to end.
while count < n:
arr[count] = 0
count += 1
# Driver code
arr = [1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9]
n = len(arr)
pushZerosToEnd(arr, n)
print("Array after pushing all zeros to end of array:")
print(arr)