human
Sort an Array based on the absolute difference of adjacent elements | Function that arrange the array such that all absolute difference between adjacent element are sorted.
To store the resultant array.
Sorting the given array in ascending order.
Variable to represent left and right ends of the given array.
Traversing the answer array in reverse order and arrange the array elements from arr [ ] in reverse order.
Inserting elements in zig - zag manner.
Displaying the resultant array.
Driver Code.
Function Call"
Solution in Python:
assistant
#Here is code to sort an Array based on the absolute difference of adjacent elements and the other steps required.
 def sortedAdjacentDifferences ( arr , n ) :
	ans = [ 0 ] * n
	arr = sorted ( arr )
	l = 0
	r = n - 1
	for i in range ( n - 1 , - 1 , - 1 ) :
		if ( i % 2 ) :
			ans [ i ] = arr [ l ]
			l += 1
		else :
			ans [ i ] = arr [ r ]
			r -= 1
	for i in range ( n ) :
		print ( ans [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 5 , - 2 , 4 , 8 , 6 , 4 , 5 ]
	n = len ( arr )
	sortedAdjacentDifferences ( arr , n )