P18028 [GeeksforGeeks] Segregate 0s and 1s5106
普及-
题目描述

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

输入格式

见题面

输出格式

见题面

样例输入
N = 5 arr[] = {0, 0, 1, 1, 0}
样例输出
0 0 0 1 1