 All Problems
Sort Colors
medium
arrays
two pointers
sorting
amazon
microsoft
google
facebook

Given an array nums with values 0, 1, and 2 (representing red, white, blue), sort it in-place so that objects of the same color are adjacent in the order 0, 1, 2. Must be one pass only.

Example 1:

Input:  2 0 2 1 1 0
Output: 0 0 1 1 2 2

Example 2:

Input:  2 0 1
Output: 0 1 2

Input format: Space-separated integers (0, 1, or 2).

Output format: Space-separated sorted integers.

Run to check your code against the sample cases, or submit to run every case