19. Airport Security Risk Sort
Difficulty: easy · Array, Sorting, Two Pointers
Problem
Airport security officials have confiscated items from passengers and dumped them into a box. Each item has a risk level of `0`, `1` or `2`. The box is an array of `N` risk values. Sort the items by risk level in ascending order (all `0`s, then all `1`s, then all `2`s) and print the result.
Input
- Line 1: the integer `N`. - Line 2: `N` space-separated integers, each `0`, `1` or `2`.
Output
Print the sorted values on one line, separated by single spaces.
Example 1
Input: 7 1 0 2 0 1 0 2 Output: 0 0 0 1 1 2 2
Explanation: Three 0s, two 1s and two 2s, printed in that order.
Example 2
Input: 10 2 1 0 2 1 0 0 1 2 0 Output: 0 0 0 0 1 1 1 2 2 2
Explanation: Four 0s, three 1s and three 2s.
Constraints
- 1 <= N <= 10^5 - Each value is 0, 1 or 2
Solutions are judged against 2 sample and 9 hidden tests. Sign in to solve it · All problems