human
Move all zeros to start and ones to end in an Array of random integers | Utility function to print the contents of an array.
Function that pushes all the zeros to the start and ones to the end of an array.
To store the count of elements which are not equal to 1.
Traverse the array and calculate count of elements which are not 1.
Now all non - ones elements have been shifted to front and ' count1' is set as index of first 1. Make all elements 1 from count to end ..
Initialize lastNonBinary position to zero.
Traverse the array and pull non - zero elements to the required indices.
Ignore the 1 's.
Mark the position Of last NonBinary integer.
Place non - zero element to their required indices.
Put zeros to start of array.
Driver code"
Solution in Python:
 ... Give me a code to move all zeros to start and ones to end in an Array of random integers and the other steps required?
assistant
#Here is code to move all zeros to start and ones to end in an Array of random integers and the other steps required.
 def printArr ( arr , n ) :
	for i in range ( n ) :
		print ( arr [ i ] , end = " ▁ " )
def pushBinaryToBorder ( arr , n ) :
	count1 = 0
	for i in range ( n ) :
		if ( arr [ i ] != 1 ) :
			arr [ count1 ] = arr [ i ]
			count1 += 1
	while ( count1 < n ) :
		arr [ count1 ] = 1
		count1 += 1
	lastNonOne = 0
	for i in range ( n - 1 , - 1 , - 1 ) :
		if ( arr [ i ] == 1 ) :
			continue
		if ( not lastNonOne ) :
			lastNonOne = i
		if ( arr [ i ] != 0 ) :
			arr [ lastNonOne ] = arr [ i ]
			lastNonOne -= 1
	while ( lastNonOne >= 0 ) :
		arr [ lastNonOne ] = 0
		lastNonOne -= 1
if __name__ == " _ _ main _ _ " :
	arr = [ 1 , 2 , 0 , 0 , 0 , 3 , 6 ] ;
	n = len ( arr ) ;
	pushBinaryToBorder ( arr , n )
	printArr ( arr , n )