human
Find permutation of [ 1 , N ] such that ( arr [ i ] != i + 1 ) and sum of absolute difference between arr [ i ] and ( i + 1 ) is minimum | Function to generate the permutation of the first N natural numbers having sum of absolute difference between element and indices as minimum.
Initialize array arr [ ] from 1 to N.
Swap alternate positions.
Check N is greater than 1 and N is odd.
Swapping last two positions.
Print the permutation.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to find permutation of [ 1 , N ] such that ( arr [ i ] != i + 1 ) and sum of absolute difference between arr [ i ] and ( i + 1 ) is minimum and the other steps required.
 def findPermutation ( N ) :
	arr = [ i + 1 for i in range ( N ) ]
	for i in range ( 1 , N , 2 ) :
		arr [ i ] , arr [ i - 1 ] = arr [ i - 1 ] , arr [ i ]
	if N % 2 and N > 1 :
		arr [ - 1 ] , arr [ - 2 ] = arr [ - 2 ] , arr [ - 1 ]
	for i in arr :
		print ( i , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
	N = 7
	findPermutation ( N )