#C6475. Rearrangement Sum Range Problem
Rearrangement Sum Range Problem
Rearrangement Sum Range Problem
You are given an array of integers and two integers L and R. Your task is to determine if the array can be rearranged such that the sum of differences between adjacent elements lies in the range \([L, R]\) (inclusive).
Let the array be \(A = [a_1,a_2,\dots,a_N]\). Define the sum of differences for a permutation \(P\) of \(A\) as \[ S(P) = \sum_{i=2}^{N} |P_i - P_{i-1}|. \]
You need to decide if there exists a permutation \(P\) of \(A\) such that \(L \le S(P) \le R\). It is guaranteed that \(N \ge 1\).
inputFormat
The input is read from standard input and consists of three lines:
- The first line contains an integer \(N\) denoting the number of elements in the array.
- The second line contains \(N\) space-separated integers, representing the elements of the array \(A\).
- The third line contains two space-separated integers, \(L\) and \(R\), defining the inclusive range.
outputFormat
Output a single line to standard output: Possible
if there exists a rearrangement that satisfies the condition, or Impossible
otherwise.
5
3 1 4 1 5
5 9
Possible