← Back

🚩 Color Band Rearranger

Dutch National Flag - 3-Way Partition

Low

0

Mid

0

High

-

Swaps

0

Step

0
πŸ”΄ Sort: 0s (Red) | 1s (White) | 2s (Blue) πŸ”΅
low 0
mid 0
high -
0 (Red)
1 (White)
2 (Blue)
Generate a color array to sort using 3 pointers!

πŸ“– Dutch National Flag (3-Way Partition)

Three Pointers: low, mid, high

β€’ arr[mid] = 0: swap with low, increment both low & mid

β€’ arr[mid] = 1: just increment mid

β€’ arr[mid] = 2: swap with high, decrement high

Time: O(n) - Single pass!

Space: O(1) - In-place