All Tracks Algorithms Sorting Merge Sort Problem

Monk and Modulo Based Sorting
Tag(s):

Algorithms, Easy, Sorting

Problem
Editorial
Analytics

Monk likes to experiment with algorithms. His one such experiment is using modulo in sorting.He describes an array modulo sorted as:
Given an integer $$k$$, we need to sort the values in the array according to their modulo with $$k$$. That is, if there are two integers $$a$$ and $$b$$, and $$a\%k < b\%k$$, then $$a$$ would come before $$b$$ in the sorted array. If $$a\%k=b\%k$$ , then the integer which comes first in the given array remains first in the sorted array.
Given an initial array, you need to print modulo sorted array.

Input:
The first line consists of two integers $$N$$ and $$k$$, $$N$$ being the number of elements in the array and $$k$$ is the number with which we need to take the modulo.
The next line consists of $$N$$ space separated integers , denoting the elements of the array $$A$$.

Output:
Print the modulo sorted array of the given array.

Constraints:
$$1 \le N \le 10^4$$
$$1 \le k \le 10^9$$
$$1 \le A[i] \le 10^9$$; $$1\le i \le N$$

SAMPLE INPUT
5 6
12 18 17 65 46
SAMPLE OUTPUT
12 18 46 17 65 
Explanation

12%6=0
18%6=0
17%6=5
65%6=5
46%6=4
So, the sorted order is: 12 18 46 17 65
12 and 18 have same result on modulo , so, 12 remains first in sorted array as it is first in given array

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: C, C++, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala 2.11.8, Swift, Visual Basic

CODE EDITOR

Initializing Code Editor...
Your Rating:

Contributor

This Problem was Asked in

HackerEarth

Challenge Name

CodeMonk (Sorting)

OTHER PROBLEMS OF THIS CHALLENGE
Notifications
View All Notifications