Loading...
Loading...
A student has a pile of n old exam papers. Each paper has a subject code. She wants to revise in a fixed order of m subjects. So the papers must be arranged like this: first all papers of the first preferred code, then all papers of the second preferred code, and so on. Papers whose code is not in the preferred list go at the end, in increasing order of code.
Print the codes of the papers in the new order. A preferred code may have no papers at all.
Example 1
Input
9 5 3 8 3 1 5 9 2 3 3 3 5 1
Output
3 3 3 5 5 1 2 8 9
All three 3s first, then both 5s, then the 1. The rest (8, 9, 2) go at the end as 2 8 9.
Example 2
Input
4 7 4 7 4 2 9 4
Output
4 4 7 7
Code 9 has no papers. Both 4s come first, then the 7s.
Try the problem first. Open one hint at a time.
The codes are small (at most 1000). Count how many papers have each code in an array of size 1001.
Walk through the preferred list. For each code, print it as many times as its count, then set its count to 0.
After that, walk the codes from 0 to 1000 and print every code that still has a count left.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor