A spreadsheet has n rows tracking two scores per student: a Quiz score and a Homework score. In each column, an entry of 0 does not mean an actual score of zero -- it marks that the item was never submitted, i.e. the cell is blank. The instructor wants to rebuild the sheet by re-sorting each column completely independently of the other (breaking any original pairing between a student's quiz and homework row): the Quiz column should be sorted so its real (nonzero) scores appear in descending order first, with every blank (0) entry pushed after all of them; the Homework column should be sorted so its real (nonzero) scores appear in ascending order first, with every blank (0) entry pushed after all of them. After independently sorting each column this way, the rebuilt sheet's row i simply pairs the i-th value of the sorted Quiz column with the i-th value of the sorted Homework column.
Line 1: a single integer n, the number of rows.
Line 2: n space-separated integers, the Quiz column (0 marks blank).
Line 3: n space-separated integers, the Homework column (0 marks blank).
Print n lines. Line i (1-indexed) contains two space-separated integers: the i-th value of the sorted Quiz column followed by the i-th value of the sorted Homework column.
Example 1
Input
3 5 0 3 0 2 4
Expected
5 2 3 4 0 0
Explanation
Quiz column [5,0,3]: nonzero values {5,3} sorted descending give [5,3], then the one blank appended gives [5,3,0]. Homework column [0,2,4]: nonzero values {2,4} sorted ascending give [2,4], then the one blank appended gives [2,4,0]. Pairing by position: row 1 is (5,2), row 2 is (3,4), row 3 is (0,0).
Example 2
Input
4 0 0 7 2 9 0 0 1
Expected
7 1 2 9 0 0 0 0
Explanation
Quiz column [0,0,7,2]: nonzero values {7,2} sorted descending give [7,2], then two blanks appended give [7,2,0,0]. Homework column [9,0,0,1]: nonzero values {9,1} sorted ascending give [1,9], then two blanks appended give [1,9,0,0]. Pairing by position gives (7,1), (2,9), (0,0), (0,0).
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →