Sort an Array of 0s and 1s
Problem Given an array containing only 0s and 1s, sort it in place so that all 0s precede all 1s.
Input / Output
- Input: array
numscontaining only the values 0 and 1. - Output: the same array, mutated in place, with every 0 before every 1.
Constraints
- Must be in place — O(1) extra space, no auxiliary array.
- Single pass preferred; a counting solution that rewrites the array is a valid but weaker answer.
- 0 <= nums.length <= 10^5.
Example
[1,0,1,0,0,1]->[0,0,0,1,1,1].- Edge cases: all-zeros
[0,0]and all-ones[1,1]are already sorted and must survive untouched; an empty array returns immediately.
asked …