All Tracks Algorithms Sorting Bucket Sort Problem

Descending Weights

Easy, Sorting


You have been given an array A of size N and an integer K. This array consists of N integers ranging from 1 to \(10^7\). Each element in this array is said to have a Special Weight. The special weight of an element \(a[i]\) is \(a[i]\%K\).

You now need to sort this array in Non-Increasing order of the weight of each element, i.e the element with the highest weight should appear first, then the element with the second highest weight and so on. In case two elements have the same weight, the one with the lower value should appear in the output first.

Input Format:

The first line consists of two space separated integers N and K. The next line consists of N space separated integers denoting the elements of array A.

Output Format:

Print N space separated integers denoting the elements of the array in the order in which they are required.


\( 1 \le N \le 10^5 \)

\( 1 \le A[i] \le 10^7 \)

\( 1 \le K \le 10^7 \)


You need to print the value of each element and not their weight.

5 2
1 2 3 4 5
1 3 5 2 4
Time Limit: 1.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: Bash, C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Swift-4.1, TypeScript, Visual Basic


Initializing Code Editor...
Your Rating:


View All Notifications