Given an array of length N consisting of only 0s and 1s in random order. Modify the array to segregate 0s on left side and 1s on the right side of the array. 
Example 1:
Input:
N = 5
arr[] = {0, 0, 1, 1, 0}
Output: 0 0 0 1 1
Example 2:
Input:
N = 4
Arr[] = {1, 1, 1, 1}
Output: 1 1 1 1
Explanation: There are no 0 in the given array, 
so the modified array is 1 1 1 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function segregate0and1() which takes arr[] and n as input parameters and modifies arr[] in-place without using any extra memory.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{7}
0 ≤ arr_{i} ≤ 1

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3



class Solution:

    def segregate0and1(self, arr, n):

        # code here

Solutions:
class Solution:

	def segregate0and1(self, arr, n):
		j = -1
		for i in range(n):
			if arr[i] < 1:
				j = j + 1
				(arr[i], arr[j]) = (arr[j], arr[i])


class Solution:

	def segregate0and1(self, arr, n):
		arr.sort()


class Solution:

	def segregate0and1(self, arr, n):
		first = 0
		last = n - 1
		while first < last:
			if arr[first] == 1:
				if arr[last] != 1:
					(arr[first], arr[last]) = (arr[last], arr[first])
				last -= 1
			else:
				first += 1


class Solution:

	def segregate0and1(self, arr, n):
		o = []
		z = []
		for i in range(len(arr)):
			if arr[i] == 0:
				z.append(arr[i])
			else:
				o.append(arr[i])
		a = z + o
		for i in range(len(arr)):
			arr[i] = a[i]


class Solution:

	def segregate0and1(self, arr, n):
		left = 0
		right = len(arr) - 1
		current = 0
		while current <= right:
			if arr[current] == 0:
				(arr[current], arr[left]) = (arr[left], arr[current])
				left += 1
				current += 1
			else:
				current += 1
		return arr


class Solution:

	def segregate0and1(self, arr, n):
		left = 0
		right = len(arr) - 1
		while left < right:
			if arr[left] == 0:
				left += 1
			elif arr[right] == 1:
				right -= 1
			else:
				(arr[left], arr[right]) = (arr[right], arr[left])
				left += 1
				right -= 1


class Solution:

	def segregate0and1(self, arr, n):
		a = 0
		b = 0
		for i in range(len(arr)):
			if arr[i] == 0:
				a += 1
			elif arr[i] == 1:
				b += 1
		k = 0
		while a > 0:
			arr[k] = 0
			k += 1
			a -= 1
		while b > 0:
			arr[k] = 1
			k += 1
			b -= 1


class Solution:

	def segregate0and1(self, arr, n):
		i = 0
		c_0 = arr.count(0)
		c_1 = arr.count(1)
		while c_0 != 0:
			arr[i] = 0
			i = i + 1
			c_0 -= 1
		while c_1 != 0:
			arr[i] = 1
			i = i + 1
			c_1 -= 1


class Solution:

	def segregate0and1(self, arr, n):
		(i, j) = (0, 0)
		while i < n and j < n:
			if arr[j] == 0:
				(arr[i], arr[j]) = (arr[j], arr[i])
				i += 1
			j += 1
		return arr


class Solution:

	def segregate0and1(self, arr, n):
		n = len(arr)
		zero = 0
		one = 1
		for i in range(0, n):
			if arr[i] == 0:
				zero += 1
			else:
				one += 1
		for x in range(0, zero):
			arr[x] = 0
		for y in range(x + 1, n):
			arr[y] = 1
		return arr


class Solution:

	def segregate0and1(self, arr, n):
		i = 0
		j = n - 1
		while i <= n - 1 and i <= j:
			if arr[i] == 0:
				i += 1
			elif arr[i] == 1 and arr[j] == 1:
				j -= 1
			else:
				k = arr[i]
				arr[i] = arr[j]
				arr[j] = k
				i += 1
				j -= 1
		return arr


class Solution:

	def segregate0and1(self, arr, n):
		left_pointer = 0
		right_pointer = len(arr) - 1
		while left_pointer < right_pointer:
			if arr[left_pointer] == 0:
				left_pointer += 1
			elif arr[right_pointer] == 1:
				right_pointer -= 1
			else:
				(arr[left_pointer], arr[right_pointer]) = (arr[right_pointer], arr[left_pointer])
				left_pointer += 1
				right_pointer -= 1


class Solution:

	def segregate0and1(self, arr, n):
		l = []
		k = []
		for i in range(n):
			if arr[i] == 0:
				l.append(arr[i])
			else:
				k.append(arr[i])
		arr[:] = l[:] + k[:]
		return arr


class Solution:

	def segregate0and1(self, arr, n):
		(i, j) = (0, 0)
		while i < n:
			if arr[i] == 1:
				i += 1
			else:
				(arr[i], arr[j]) = (arr[j], arr[i])
				i += 1
				j += 1


class Solution:

	def segregate0and1(self, arr, n):
		st = 0
		nd = len(arr) - 1
		while st != nd:
			if arr[st] == 0:
				st += 1
			elif arr[nd] == 1:
				nd -= 1
			else:
				(arr[st], arr[nd]) = (arr[nd], arr[st])


class Solution:

	def segregate0and1(self, a, n):
		zi = 0
		for i in range(n):
			if a[i] == 0:
				(a[i], a[zi]) = (a[zi], a[i])
				zi += 1


class Solution:

	def segregate0and1(self, arr, n):
		a = arr.sort()
		return a


class Solution:

	def segregate0and1(self, arr, n):
		k = sorted(arr)
		arr[:] = k


class Solution:

	def segregate0and1(self, a, n):
		i = 0
		j = n - 1
		while i <= j:
			if a[i] == 0:
				i += 1
			elif a[j] == 1:
				j -= 1
			elif a[i] == 1:
				(a[i], a[j]) = (a[j], a[i])
				j -= 1
			else:
				(a[i], a[j]) = (a[j], a[i])
				i += 1
		return a


class Solution:

	def segregate0and1(self, arr, n):
		nextzero = 0
		for i in range(n):
			if arr[i] == 0:
				temp = arr[nextzero]
				arr[nextzero] = arr[i]
				arr[i] = temp
				nextzero += 1


class Solution:

	def segregate0and1(self, arr, n):
		start = 0
		i = 0
		end = n - 1
		while i != end:
			if arr[i] == 0:
				(arr[start], arr[i]) = (arr[i], arr[start])
				start += 1
				i += 1
			else:
				(arr[i], arr[end]) = (arr[end], arr[i])
				end -= 1


class Solution:

	def segregate0and1(self, arr, n):
		(left, right) = (0, n - 1)
		while left < right:
			if arr[left] == 0:
				left += 1
				continue
			if arr[right] == 1:
				right -= 1
				continue
			else:
				(arr[left], arr[right]) = (arr[right], arr[left])


class Solution:

	def segregate0and1(self, arr, n):
		(i, x) = (0, 0)
		while i < n:
			if i <= x:
				i += 1
			elif arr[x] == 0:
				x += 1
			elif arr[i] == 0:
				arr[x] = 0
				arr[i] = 1
				x += 1
				i += 1
			else:
				i += 1


class Solution:

	def segregate0and1(self, arr, n):
		count0 = 0
		for i in arr:
			if i == 0:
				count0 += 1
		for i in range(count0):
			arr[i] = 0
		for i in range(count0, n):
			arr[i] = 1
		return arr


class Solution:

	def segregate0and1(self, arr, n):
		count = 0
		for num in arr:
			if num == 0:
				arr[count] = 0
				count += 1
		while count < n:
			arr[count] = 1
			count += 1
		return


class Solution:

	def segregate0and1(self, arr, n):
		low = 0
		high = n - 1
		mid = 0
		while mid <= high:
			if arr[mid] == 0:
				(arr[low], arr[mid]) = (arr[mid], arr[low])
				low += 1
				mid += 1
			elif arr[mid] == 1:
				(arr[mid], arr[high]) = (arr[high], arr[mid])
				high -= 1
			else:
				mid += 1
		return arr


class Solution:

	def segregate0and1(self, arr, n):
		l = []
		for i in arr:
			if i == 0:
				l.append(i)
		for i in arr:
			if i != 0:
				l.append(i)
		for i in range(len(l)):
			arr[i] = l[i]


class Solution:

	def segregate0and1(self, arr, n):
		one = arr.count(1)
		zero = arr.count(0)
		list1 = [0 for i in range(1, zero + 1)] + [1 for i in range(1, one + 1)]
		for i in range(len(arr)):
			arr[i] = list1[i]


class Solution:

	def segregate0and1(self, arr, n):
		zeroes = 0
		ones = 0
		for i in range(n):
			if arr[i] == 0:
				zeroes += 1
			else:
				ones += 1
		i = 0
		while i < n:
			if zeroes > 0:
				zeroes -= 1
				arr[i] = 0
			elif zeroes == 0 and ones > 0:
				ones -= 1
				arr[i] = 1
			i += 1
		return arr


class Solution:

	def segregate0and1(self, arr, n):
		(low, high) = (0, n - 1)
		while low <= high:
			if arr[low] == 0:
				low += 1
			else:
				(arr[low], arr[high]) = (arr[high], arr[low])
				high -= 1


class Solution:

	def segregate0and1(self, arr, n):
		arr[:] = [0] * arr.count(0) + [1] * arr.count(1)


class Solution:

	def segregate0and1(self, arr, n):
		s = 0
		e = len(arr) - 1
		while s < e:
			if arr[s] == 0:
				s += 1
			if arr[e] == 1:
				e -= 1
			else:
				(arr[s], arr[e]) = (arr[e], arr[s])
		return arr


class Solution:

	def segregate0and1(self, arr, n):
		l = 0
		r = n - 1
		while l < r:
			while arr[l] != 1 and l < r:
				l += 1
			while arr[r] != 0 and l < r:
				r -= 1
			(arr[l], arr[r]) = (arr[r], arr[l])
			l += 1
			r -= 1


class Solution:

	def segregate0and1(self, arr, n):
		a = []
		b = []
		for i in arr:
			if i == 0:
				a.append(i)
			else:
				b.append(i)
		arr[:] = a + b
		return arr


class Solution:

	def segregate0and1(self, arr, n):
		zero = []
		one = []
		for i in arr:
			if i == 0:
				zero.append(i)
			else:
				one.append(i)
		i = 0
		j = 0
		while j < len(zero):
			arr[i] = zero[j]
			i += 1
			j += 1
		j = 0
		while i < n:
			arr[i] = one[j]
			i += 1
			j += 1


class Solution:

	def segregate0and1(self, arr, n):
		arr1 = []
		arr2 = []
		for i in arr:
			if i == 0:
				arr1.append(i)
			else:
				arr2.append(i)
		arr[:] = arr1 + arr2


class Solution:

	def segregate0and1(self, arr, n):
		zeros = []
		ones = []
		for i in arr:
			if i == 0:
				zeros.append(i)
			else:
				ones.append(i)
		arr[:] = zeros + ones


class Solution:

	def segregate0and1(self, arr, n):
		i = 0
		j = 0
		while j < n:
			if arr[i] == 1:
				arr.append(arr.pop(i))
				i -= 1
			i += 1
			j += 1


class Solution:

	def segregate0and1(self, arr, n):
		zero = 0
		i = 0
		while arr[zero] == 0:
			zero += 1
			i += 1
		while i < n:
			if arr[i] == 0:
				(arr[i], arr[zero]) = (arr[zero], arr[i])
				zero += 1
			i += 1


class Solution:

	def segregate0and1(self, arr, n):
		l = [x for x in arr if x == 0]
		m = [y for y in arr if y == 1]
		l = l + m
		arr[:] = l[:]
		return arr


class Solution:

	def segregate0and1(self, arr, n):
		lst = [0] * n
		val = arr.count(1)
		st = n - val
		for i in range(st, n):
			lst[i] = 1
		arr[:] = lst


from collections import Counter

class Solution:

	def segregate0and1(self, arr, n):
		rep = cur = 0
		while cur != len(arr):
			if arr[cur] == 0:
				(arr[rep], arr[cur]) = (arr[cur], arr[rep])
				rep += 1
			cur += 1


from collections import Counter

class Solution:

	def segregate0and1(self, arr, n):
		f = Counter(arr)
		(z, o) = (f[0], f[1])
		i = 0
		while z != 0:
			arr[i] = 0
			i += 1
			z -= 1
		while o != 0:
			arr[i] = 1
			i += 1
			o -= 1


class Solution:

	def segregate0and1(self, arr, n):
		zeros = arr.count(0)
		ones = arr.count(1)
		for i in range(zeros):
			arr[i] = 0
		for j in range(-1, -ones - 1, -1):
			arr[j] = 1
		return arr


class Solution:

	def segregate0and1(self, arr, n):
		count = 0
		for i in range(n):
			if arr[i] == 0:
				arr[count] = arr[i]
				count += 1
		while count < n:
			arr[count] = 1
			count += 1
		return arr


class Solution:

	def segregate0and1(self, arr, n):
		count = arr.count(1)
		for i in range(n):
			if i < n - count:
				arr[i] = 0
			else:
				arr[i] = 1


class Solution:

	def segregate0and1(self, arr, n):
		l = []
		m = []
		for i in range(n):
			if arr[i] == 0:
				l.append(arr[i])
			if arr[i] == 1:
				m.append(arr[i])
		arr[:] = l[:] + m[:]
		return arr


class Solution:

	def segregate0and1(self, a, n):
		j = 0
		for i in range(n):
			if a[i] == 0:
				(a[i], a[j]) = (a[j], a[i])
			if a[j] == 1:
				pass
			else:
				j += 1


class Solution:

	def segregate0and1(self, arr, n):
		res = []
		for i in arr:
			if i == 0:
				res.append(i)
		for i in arr:
			if i == 1:
				res.append(i)
		arr[:] = res[:]


class Solution:

	def segregate0and1(self, arr, n):
		l = 0
		r = n - 1
		while l < r:
			if arr[l] < arr[r]:
				l += 1
			elif arr[l] > arr[r]:
				(arr[l], arr[r]) = (arr[r], arr[l])
				l += 1
				r -= 1
			elif arr[l] == arr[r] == 1:
				r -= 1
			elif arr[l] == arr[r] == 0:
				l += 1