Give numbers space number
Given a list of integers, adjust each number by at most ±1 so that in the resulting list, each number is at least 2 apart from each of its neighbors. That each, each entry n
can be replaced with n-1
, n
, or n+1
, and any two adjacent entries x
and y
must have abs(x-y) ≥ 2
. The output is a list of the same length as the input.
You will not get an input where this is impossible, such as [5, 4, 4, 5]
. The input will have at least two elements.
Examples (other outputs are possible):
[-5, -6] -> [-5, -7]
[1, 1, 1] -> [2, 0, 2]
[2, 2, 3, 3] -> [3, 1, 4, 2]
[0, 5, -1] -> [0, 5, -1]
[4, 4, 4, 4, 4, 4, 4, 3, 3, 3] -> [5, 3, 5, 3, 5, 3, 5, 2, 4, 2]
[4, 4, 4, 4, 4, 4, 4, 5, 5, 5] -> [3, 5, 3, 5, 3, 5, 3, 6, 4, 6]
[1, 1, 2, 4, 6, 7, 7] -> [2, 0, 2, 4, 6, 8, 6]