Segregate 0s and 1s
Easy Accuracy: 59.27% Submissions: 28154 Points: 2

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:

N = 5
arr[] = {0, 0, 1, 1, 0}
Output: 0 0 0 1 1

Example 2:

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 as input parameters and modifies arr[] in-place without using any extra memory.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

1 ≤ N ≤ 107
0 ≤ arri ≤ 1

We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

Segregate 0s and 1s

Output Window