k-Sort
InterviewTime limit2sMemory limit1024 MB
Given an array and k, decide whether the array can be sorted when only elements with equal remainders mod k may be swapped.
- Level
Easy3 of 10
- Topics
- Sorting, Implementation
- Solved
- No attempts yet
Problem
This year Grisha entered the University of IT. The university offers many new subjects, some interesting and some not. Grisha especially likes the course "Algorithms and Data Structures". The last lecture covered sorting algorithms.
Grisha is an ambitious young man who wants to invent his own sorting algorithm and name it after his beloved grandfather. Inspired by Knuth's multi-volume series, he decided to modify an existing sorting algorithm for natural numbers with the following restriction. Two elements can be swapped only if they are congruent modulo some natural number , that is, they leave the same remainder when divided by .
Check whether the new version of the algorithm can sort the given array of natural numbers.
Input
The first line contains two integers () and (), the number of elements in the array and the number used for comparing the elements modulo .
The second line contains integers , the elements of the array ().
Output
Print "YES" if the algorithm can sort the given array, and "NO" otherwise.