Sort Colors

Try to solve the Sort Colors problem.

Statement

Given an array, colors, which contains a combination of the following three elements:

  • 00 (representing red)

  • 11 (representing white)

  • 22 (representing blue)

Sort the array in place so that the elements of the same color are adjacent, with the colors in the order of red, white, and blue. To improve your problem-solving skills, do not utilize the built-in sort function.

Constraints:

  • 1≤1 \leq colors.length ≤103\leq 10^3
  • colors[i] can only contain 00s, 11s, or 22s.

Examples

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy