human
Write me a code to rearrange an array in maximum minimum form and the other steps required given the following:
Rearrange an array in maximum minimum form | Set 2 ( O ( 1 ) extra space ) | Prints max at first position , min at second position second max at third position , second min at fourth position and so on ..
Initialize index of first minimum and first maximum element.
Store maximum element of array.
Traverse array elements.
At even index : we have to put maximum element.
At odd index : we have to put minimum element.
array elements back to it 's original form.
Driver Code"
Solution in Python:
assistant
#Here is code to rearrange an array in maximum minimum form and the other steps required.
 def rearrange ( arr , n ) :
	max_idx = n - 1
	min_idx = 0
	max_elem = arr [ n - 1 ] + 1
	for i in range ( 0 , n ) :
		if i % 2 == 0 :
			arr [ i ] += ( arr [ max_idx ] % max_elem ) * max_elem
			max_idx -= 1
		else :
			arr [ i ] += ( arr [ min_idx ] % max_elem ) * max_elem
			min_idx += 1
	for i in range ( 0 , n ) :
		arr [ i ] = arr [ i ] / max_elem
arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
n = len ( arr )
print ( " Original ▁ Array " )
for i in range ( 0 , n ) :
	print ( arr [ i ] , end = " ▁ " )
rearrange ( arr , n )
print ( " Modified Array " )
for i in range ( 0 , n ) :
	print ( int ( arr [ i ] ) , end = " ▁ " )