Consider an array of size n. The values of the array are either 0 or 1.Write a program to move zeros to the beginning and all ones to the end in the same array without using an extra array.
input: arr[] = {1, 0, 1, 0, 0, 1}
output: arr[] = {0, 0, 0, 1, 1, 1}
Solution in python
def move(ar=[1,0,1,1 ,0, 1, 0, 0, 1,0,0]): for i in range(ar.count(1)): index_of_1 = ar.index(1) # find the index of the first occurrence of 1 ar.append(ar.pop(index_of_1)) #add all ones to the end of array/list return ar >>>[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1] #output