Sort 0s, 1s and 2s
Medium 40 XPArraysTwo PointersSorting
Commonly asked at Amazon, Microsoft, Adobe
Read a list containing only 0, 1 and 2. Print it sorted without using sort() — try a single pass (the Dutch National Flag algorithm).
- Input
- One line of space-separated 0s, 1s and 2s.
- Output
- The sorted list.
Sample 1
Input
2 0 2 1 1 0
Output
0 0 1 1 2 2
Read input with input() (no prompt message) and print only the answer. Run tries the Input box; Submit checks all test cases.
Output Loading Python (first time takes a few seconds)
Press Run to see the result here.The solution unlocks after 3 submissions.