Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Sort Colors

Medium
two-pointersarraysorting

Given an array nums with values 0, 1, and 2 representing red, white, and blue, sort them in place so that same colors are adjacent in the order red → white → blue. Do it in one pass without the library sort. Return the sorted array.

Input: one line of space-separated integers (each 0, 1, or 2). Output: the sorted array, space-separated.

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

Constraints

  • 1 ≤ nums.length ≤ 300
  • nums[i] ∈ {0, 1, 2}
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.