# Segregate 0s and 1s in an array

You are given an array of 0s and 1s in random order. Segregate 0s on left side and 1s on right side of the array. Traverse array only once.

```Input array   =  [0, 1, 0, 1, 0, 0, 1, 1, 1, 0]
Output array =  [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
```

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

Method 1 (Count 0s or 1s)
Thanks to Naveen for suggesting this method.
1) Count the number of 0s. Let count be C.
2) Once we have count, we can put C 0s at the beginning and 1s at the remaining n – C positions in array.

Time Complexity : O(n)

```// Java code to Segregate 0s and 1s in an array
class GFG {

// function to segregate 0s and 1s
static void segregate0and1(int arr[], int n)
{
int count = 0; // counts the no of zeros in arr

for (int i = 0; i < n; i++) {
if (arr[i] == 0)
count++;
}

// loop fills the arr with 0 until count
for (int i = 0; i < count; i++)
arr[i] = 0;

// loop fills remaining arr space with 1
for (int i = count; i < n; i++)
arr[i] = 1;
}

// function to print segregated array
static void print(int arr[], int n)
{
System.out.print("Array after segregation is ");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}

// driver function
public static void main(String[] args)
{
int arr[] = new int[]{ 0, 1, 0, 1, 1, 1 };
int n = arr.length;

segregate0and1(arr, n);
print(arr, n);

}
}

// This code is contributed by Kamal Rawal
```

Output :

```Array after segregation is 0 0 1 1 1 1
```

The method 1 traverses the array two times. Method 2 does the same in a single pass.

Method 2 (Use two indexes to traverse)
Maintain two indexes. Initialize first index left as 0 and second index right as n-1.

Do following while left < right
a) Keep incrementing index left while there are 0s at it
b) Keep decrementing index right while there are 1s at it
c) If left < right then exchange arr[left] and arr[right]

Implementation:

## C/C++

```// C program to sort a binary array in one pass
#include<stdio.h>

/*Function to put all 0s on left and all 1s on right*/
void segregate0and1(int arr[], int size)
{
/* Initialize left and right indexes */
int left = 0, right = size-1;

while (left < right)
{
/* Increment left index while we see 0 at left */
while (arr[left] == 0 && left < right)
left++;

/* Decrement right index while we see 1 at right */
while (arr[right] == 1 && left < right)
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]*/
if (left < right)
{
arr[left] = 0;
arr[right] = 1;
left++;
right--;
}
}
}

/* driver program to test */
int main()
{
int arr[] = {0, 1, 0, 1, 1, 1};
int i, arr_size = sizeof(arr)/sizeof(arr[0]);

segregate0and1(arr, arr_size);

printf("Array after segregation ");
for (i = 0; i < 6; i++)
printf("%d ", arr[i]);

getchar();
return 0;
}
```

## Java

```class Segregate
{
/*Function to put all 0s on left and all 1s on right*/
void segregate0and1(int arr[], int size)
{
/* Initialize left and right indexes */
int left = 0, right = size - 1;

while (left < right)
{
/* Increment left index while we see 0 at left */
while (arr[left] == 0 && left < right)
left++;

/* Decrement right index while we see 1 at right */
while (arr[right] == 1 && left < right)
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]*/
if (left < right)
{
arr[left] = 0;
arr[right] = 1;
left++;
right--;
}
}
}

/* Driver Program to test above functions */
public static void main(String[] args)
{
Segregate seg = new Segregate();
int arr[] = new int[]{0, 1, 0, 1, 1, 1};
int i, arr_size = arr.length;

seg.segregate0and1(arr, arr_size);

System.out.print("Array after segregation is ");
for (i = 0; i < 6; i++)
System.out.print(arr[i] + " ");
}
}
```

## Python

```# Python program to sort a binary array in one pass

# Function to put all 0s on left and all 1s on right
def segregate0and1(arr, size):
# Initialize left and right indexes
left, right = 0, size-1

while left < right:
# Increment left index while we see 0 at left
while arr[left] == 0 and left < right:
left += 1

# Decrement right index while we see 1 at right
while arr[right] == 1 and left < right:
right -= 1

# If left is smaller than right then there is a 1 at left
# and a 0 at right. Exchange arr[left] and arr[right]
if left < right:
arr[left] = 0
arr[right] = 1
left += 1
right -= 1

return arr

# driver program to test
arr = [0, 1, 0, 1, 1, 1]
arr_size = len(arr)
print("Array after segregation")
print(segregate0and1(arr, arr_size))

# This code is contributed by Pratik Chhajer
```