human
Rearrange array by interchanging positions of even and odd elements in the given array | Function to replace odd elements with even elements and vice versa.
Length of the array.
Traverse the given array.
If arr [ i ] is visited.
Find the next odd element.
Find next even element.
Mark them visited.
Swap them.
Print the final array.
Driver Code.
Given array arr.
Function Call"
Solution in Python:
 ... Can you provide a code to rearrange array by interchanging positions of even and odd elements in the given array and the other steps required?
assistant
#Here is code to rearrange array by interchanging positions of even and odd elements in the given array and the other steps required.
 def swapEvenOdd ( arr ) :
	n = len ( arr )
	o = - 1
	e = - 1
	for i in range ( n ) :
		if ( arr [ i ] < 0 ) :
			continue
		r = - 1
		if ( arr [ i ] % 2 == 0 ) :
			o += 1
			while ( arr [ o ] % 2 == 0 or arr [ o ] < 0 ) :
				o += 1
			r = o
		else :
			e += 1
			while ( arr [ e ] % 2 == 1 or arr [ e ] < 0 ) :
				e += 1
			r = e
		arr [ i ] *= - 1
		arr [ r ] *= - 1
		tmp = arr [ i ]
		arr [ i ] = arr [ r ]
		arr [ r ] = tmp
	for i in range ( n ) :
		print ( ( - 1 * arr [ i ] ) , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 1 , 3 , 2 , 4 ]
	swapEvenOdd ( arr )