#K89107. Rearrange Array to Arithmetic Sequence
Rearrange Array to Arithmetic Sequence
Rearrange Array to Arithmetic Sequence
Given an array of n integers and a common difference d, determine whether it is possible to rearrange the array to form a valid arithmetic sequence. An arithmetic sequence is defined by the formula:
$a,\, a+d,\, a+2d,\, \dots,\, a+(n-1)d$
where a is the smallest element in the sequence. If a valid rearrangement exists, output the sequence in increasing order. Otherwise, output IMPOSSIBLE
.
inputFormat
The input consists of two lines:
- The first line contains two integers n and d, where n is the number of elements and d is the common difference.
- The second line contains n space-separated integers representing the array elements.
outputFormat
If it is possible to rearrange the array into an arithmetic sequence, output n space-separated integers corresponding to the sequence in increasing order. Otherwise, output the string IMPOSSIBLE
.
The arithmetic sequence should follow the formula:
where a is the smallest element among the given numbers.## sample
5 2
3 7 1 9 5
1 3 5 7 9