Problem
Ask
Submissions

Problem: Sort Colors

Medium
30 min
Explore how to efficiently sort an array of colors represented by 0s, 1s, and 2s using the two pointers pattern. Understand the problem constraints and implement an in-place solution without using extra space or built-in sorting functions.

Statement

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

  • 0 (Representing red)

  • 1 (Representing white)

  • 2 (Representing blue)

Sort the array in place so that the elements of the same color are adjacent, and the final order is: red (0), then white (1), and then blue (2).

Note: You are not allowed to use any built-in sorting functions. The goal is to solve this efficiently without extra space.

Constraints:

  • nn ==== colors.length

  • 11 \leq nn 300\leq 300

  • colors[i] is either 0, 1, or 2

Problem
Ask
Submissions

Problem: Sort Colors

Medium
30 min
Explore how to efficiently sort an array of colors represented by 0s, 1s, and 2s using the two pointers pattern. Understand the problem constraints and implement an in-place solution without using extra space or built-in sorting functions.

Statement

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

  • 0 (Representing red)

  • 1 (Representing white)

  • 2 (Representing blue)

Sort the array in place so that the elements of the same color are adjacent, and the final order is: red (0), then white (1), and then blue (2).

Note: You are not allowed to use any built-in sorting functions. The goal is to solve this efficiently without extra space.

Constraints:

  • nn ==== colors.length

  • 11 \leq nn 300\leq 300

  • colors[i] is either 0, 1, or 2