I'm working through the CSES problem set as I learn to program, and this solution was accepted, but I'm not quite happy with it. I just know there's a faster way to check whether or not a solution's valid instead of looping through every element, and I'm sure there're plenty more improvements that could be made. Any feedback is appreciated!
Problem: A permutation of integers 1,2,…,n is called beautiful if there are no adjacent elements whose difference is 1. Given n, construct a beautiful permutation if such a permutation exists. If there are several solutions, you may print any of them. If there are no solutions, print "NO SOLUTION".
def check_solution(sol): last = sol for num in sol: if num - last == 1 or num - last == -1: return False last = num return True a = [int(x) for x in range(1, int(input())+1)] b =  even = a[1::2] odd = a[::2] b.extend(even) b.extend(odd) if check_solution(b): print(*b) else: print("NO SOLUTION")