#K67242. Non-adjacent Room Assignment
Non-adjacent Room Assignment
Non-adjacent Room Assignment
You are given a total of n rooms, numbered from 1 to n, and a list of room requests. Each request is represented by a unique room number. Your task is to determine whether it is possible to assign the requested rooms such that no two assigned rooms are consecutive. In other words, in the sorted order of requested room numbers, the condition $$r_i - r_{i-1} \neq 1$$ must hold for every adjacent pair.
If the assignment is valid, output the list of assigned rooms in ascending order in a Python-style list format (for example, [1, 3, 5]). If it is not possible (i.e. if any two requested rooms are consecutive), output an empty list: []
.
Note: If there are no room requests (i.e. m = 0), consider the assignment valid and output an empty list.
inputFormat
The input is given via standard input (stdin) and consists of multiple lines:
- The first line contains an integer
n
, representing the total number of rooms. - The second line contains an integer
m
, the number of room requests. - The third line contains
m
space-separated integers, each representing a requested room number.
outputFormat
Print the room assignments in ascending order as a Python-style list (e.g., [1, 3, 5]). If it is impossible to assign the room without violating the non-adjacency constraint, print an empty list: []
.## sample
5
3
1 3 5
[1, 3, 5]
</p>