#K63122. Reorganize Array
Reorganize Array
Reorganize Array
You are given an array of n integers and an integer k. Your task is to reorder the array such that for every adjacent pair of elements, the sum is not divisible by k. In other words, for every adjacent pair \(a_i\) and \(a_{i+1}\) (for \(1 \leq i < n\)), the condition
[ (a_i + a_{i+1}) \mod k \neq 0 ]
must hold. If there exists no such reordering, output -1
.
Note: If there are multiple valid arrangements, you may output any one of them.
inputFormat
The input is read from standard input and has the following format:
- The first line contains two space-separated integers:
n
(the number of elements in the array) andk
(the divisor). - The second line contains
n
space-separated integers representing the array.
outputFormat
Output a single line to standard output. If a valid reordering exists, print the reordered array as space‑separated integers. Otherwise, print -1
.
4 3
1 4 2 3
1 4 3 2