Practice/Google/Leetcode 75. Sort Colors
CodingMust
Given an array containing only the integers 0, 1, and 2, rearrange the elements in-place so that all 0s appear first, followed by all 1s, and finally all 2s. The relative ordering within each group doesn't matter.
Your goal is to solve this with a single pass through the array and using only constant extra space. Do not use any built-in sorting functions.
Example 1:
Input: arr = [2, 0, 1, 2, 1, 0] Output: [0, 0, 1, 1, 2, 2] Explanation: All 0s are moved to the beginning, 1s in the middle, and 2s at the end
Example 2:
Input: arr = [2, 0, 2, 1, 1, 0] Output: [0, 0, 1, 1, 2, 2] Explanation: The array is partitioned into three sections
Example 3:
Input: arr = [0] Output: [0] Explanation: Single element array is already sorted