Typical example
Input
nums = [2, 0, 2, 1, 1, 0]
Output
[0, 0, 1, 1, 2, 2]
A single-pass in-place algorithm groups 0s, then 1s, then 2s.
Full lesson preview
Sort an array containing 0s, 1s, and 2s in a single pass using constant extra space.
Problem statement
Task
Examples
Input
nums = [2, 0, 2, 1, 1, 0]
Output
[0, 0, 1, 1, 2, 2]
A single-pass in-place algorithm groups 0s, then 1s, then 2s.
Input format
Output format
Constraints
Samples
Input
nums = [2, 0, 1]
Output
[0, 1, 2]
Sorted in-place to [0,1,2].