#K38237. Rearrange Array to Avoid Arithmetic Sequences

    ID: 26154 Type: Default 1000ms 256MiB

Rearrange Array to Avoid Arithmetic Sequences

Rearrange Array to Avoid Arithmetic Sequences

You are given three inputs: an integer n, an integer k (which represents the number of distinct values available in the sequence), and a list of n integers. Your task is to rearrange the array such that no three consecutive numbers form an arithmetic progression.

An arithmetic progression is defined such that for any three consecutive numbers \(a, b, c\), they must not satisfy \(b - a = c - b\). If a valid rearrangement exists, output the rearranged array; otherwise, output an empty array: [].

Note: The input is provided via standard input and the result should be printed to standard output.

inputFormat

The input is given from standard input in the following format: The first line contains two integers n and k separated by a space. The second line contains n integers separated by spaces representing the array.

outputFormat

If a valid rearrangement exists, output a single line with the rearranged array, with the integers separated by a space. If no valid rearrangement exists, output "[]".## sample

5 5
1 2 3 4 5
1 2 4 3 5

</p>