





Please login
Prime

Prepinsta Prime
Video courses for company/skill based Preparation
(Check all courses)
Get Prime Video
Prime

Prepinsta Prime
Purchase mock tests for company/skill building
(Check all mocks)
Get Prime mock
Segregate 0’s and 1’s in array

Segregate 0’s and 1’s in array in Java
Problem statement:
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]
Algorithm:
- START
- Count the number of 0s. Let count be C.
- Once we have counted, we can put C 0s at the beginning and 1s at the remaining n – C positions in the array.
- END
Java code :
// Java code to Segregate 0s and 1s in an array
class prepinsta {
// function to segregate 0s and 1s
static void segregate1(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] + ” “);
}
public static void main(String[] args)
{
int arr[] = new int[]{ 0, 1, 0, 1, 1, 1 };
int n = arr.length;
segregate1(arr, n);
print(arr, n);
}
}
Output:
Array after segregation is 0 0 1 1 1 1
Login/Signup to comment