Given a static-sized array of integers arr, move all zeroes in the array to the end of the array. You should preserve the relative order of items in the array.
We should implement a solution that is more efficient than a naive brute force.
Examples:
input: arr = [1, 10, 0, 2, 8, 3, 0, 0, 6, 4, 0, 5, 7, 0]
output: [1, 10, 2, 8, 3, 6, 4, 5, 7, 0, 0, 0, 0, 0]
def moveZerosToEnd(arr):
#pass
nonzeros=[]
zeros=[]
#arr = [1, 10, 0, 2, 8, 3, 0, 0, 6, 4, 0, 5, 7, 0]
for i in arr:
if i==0:
zeros.append(i)
else:
nonzeros.append(i)
for i in zeros:
nonzeros.append(i)
#print(nonzeros)
return nonzeros