Back to DSA

Sort Colors

medium
Acceptance: 48%
SortingArraysTwo Pointers

An array contains n elements, each being 0, 1, or 2 (representing three categories). Rearrange the array in-place so that all 0s come first, then all 1s, then all 2s. Accomplish this in a single pass through the array without using a sorting library.

Examples

Example 1:
Input:nums = [1,0,2,1,0,2]
Output:[0,0,1,1,2,2]
Example 2:
Input:nums = [0,2,1]
Output:[0,1,2]

Hints

00:00
1234567