human
Write me a code to segregate 0 s and 1 s in an array and the other steps required given the following:
Segregate 0 s and 1 s in an array | Function to put all 0 s on left and all 1 s on right.
Initialize left and right indexes.
Increment left index while we see 0 at left.
Decrement right index while we see 1 at right.
If left is smaller than right then there is a 1 at left and a 0 at right . Exchange arr [ left ] and arr [ right ].
driver program to test"
Solution in Python:
assistant
#Here is code to segregate 0 s and 1 s in an array and the other steps required.
 def segregate0and1 ( arr , size ) :
	left , right = 0 , size - 1
	while left < right :
		while arr [ left ] == 0 and left < right :
			left += 1
		while arr [ right ] == 1 and left < right :
			right -= 1
		if left < right :
			arr [ left ] = 0
			arr [ right ] = 1
			left += 1
			right -= 1
	return arr
arr = [ 0 , 1 , 0 , 1 , 1 , 1 ]
arr_size = len ( arr )
print ( " Array ▁ after ▁ segregation " )
print ( segregate0and1 ( arr , arr_size ) )