Sort 0s, 1s and 2s in one pass
The array holds only 0s, 1s and 2s. Sort it in a single pass without a sorting function (all 0s, then 1s, then 2s) and print it.
Reported in: TCS NQT (2026) (memory-based)
Input
First line: N. Second line: N values (0, 1 or 2).
Output
The sorted array.
Constraints
1 ≤ N ≤ 10^5.
Example 1
Input:
6
0 1 2 1 0 2
Output:
0 0 1 1 2 2Example 2
Input:
5
2 2 1 0 0
Output:
0 0 1 2 2Hint: Keep three pointers: low (next place for a 0), mid (current) and high (next place for a 2). Swap a 0 to low, skip a 1, swap a 2 to high (without moving mid).