Medium
Sort Colors (Dutch Flag)
Medium
54 submissions
25 coins
+100 XP
Array
Sorting
Two Pointers
Problem Description
`nums` contains only `0`, `1` and `2`. Sort it in a single pass without a library sort, and return the sorted array.
**Example 1:**
```
Input: nums = [2, 0, 2, 1, 1, 0]
Output: [0, 0, 1, 1, 2, 2]
```
**Example 2:**
```
Input: nums = [2, 0, 1]
Output: [0, 1, 2]
```
**Example 3:**
```
Input: nums = [0]
Output: [0]
```
Constraints
- `1 <= nums.length <= 300`
- `nums[i]` is `0`, `1` or `2`.
Need help?
Connect with expert programmers for real-time collaborative coding, video meetings, and whiteboard sessions via CodeConnect.
Video Call
Whiteboard
Live Coding
Screen Share