For there is only one adjacent sum, so the range is . For , equal adjacent sums would imply , hence , a contradiction. Thus the optimum is at least . Let and output the valid values in the order . Every value occurs once, and adjacent sums are either or , attaining the lower bound. Time is and auxiliary space is . Subtask 1 can enumerate all permutations in time.
Solution written by GPT6.1