Sort Colors (Dutch Flag)
mediumSort array of 0s, 1s, 2s in-place using three pointers
Sort Colors (Dutch Flag)
Key Insight
Three pointers: low for 0s, mid scans, high for 2s. One pass O(n).
Step 1Setup
low
mid
2
00
12
21
31
4high
0
5low=0 (0s go here), mid=0 (scanner), high=5 (2s go here)
1 / 5
Learn the Pattern
Two PointersUse two pointers to traverse an array or string, reducing time complexity from O(n^2) to O(n) for problems involving pairs or subarrays.
SortingUse sorting as a preprocessing step to simplify problems, or apply partition logic for in-place rearrangement. Many interview problems become trivial once the array is sorted.