Another Sorting

View as PDF

Submit solution

Points: 5 (partial)
Time limit: 1.0s
Memory limit: 64M

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

Sorting seems to be a big topic right now, so magicalsoup decides to create his own kind of sorting. Since he can't really code, he asks you to help him code his sorting algorithm. His sorting algorithm goes likes this:

He sorts the numbers by its unit digit in ascending order. For example, if the numbers were 12, 11, 13:

The numbers would be 11, 12, 13.

If there are ties, break them by putting the bigger number first.

For example, given the array 59 107 99 27 13 47 51.

The array will be first sorted by their units digit, so the array becomes: 51 13 107 27 47 59 99.

Then the array will be sorted in descending order, so the array now becomes 51 13 107 47 27 99 59.

Given an integer N and N integers, please help magicalsoup sort the array with his algorithm!


Subtask Marks N
1 30\% 1 \le N \le 10^3
2 70\% 1 \le N \le 10^5

Input Specification

First line: an integer N.

The next line will contain N space separated integers, a_i (1 \le a_i \le 10^6) representing the elements of the array.

Output Specification

Output the sorted array on one line, with a space separating each of the elements.

Sample Input

33 33 88 88 83 38

Sample Output

83 33 33 88 88 38

Sample Explanation

The array will first be sorted by its units digit, so it becomes 33 33 83 88 88 38.

Then it's sorted by its value in descending order, so now it becomes 83 33 33 88 88 38.


There are no comments at the moment.