The equation has a real root whenever its discriminant is nonnegative. In particular, this is always true when .
We therefore arrange the sequence so that values two positions apart have opposite signs. Use the following sign pattern:
After the initial zero, the length- pattern repeats. Two positions apart in this pattern always have opposite signs, while a pair involving the first value has product zero.
Among the remaining positions, exactly are positive and are negative. Put into the positive positions and into the negative positions. Thus every integer from to is used exactly once.
For any three consecutive values , the construction gives . Hence
so every required equation has at least one real root.
The time complexity is , equal to the output size, and the additional space complexity is .
Solution written by GPT6