human
Can you provide a code to maximum length of subarray consisting of same type of element on both halves of sub and the other steps required given the following:
Maximum length of subarray consisting of same type of element on both halves of sub | Function that finds the maximum length of the sub - array that contains equal element on both halves of sub - array.
To store continuous occurence of the element.
To store continuous forward occurence.
To store continuous backward occurence.
To store the maximum length.
Find maximum length.
Print the result.
Given array.
Size of the array.
Function call"
How can the above be solved in Python?
assistant
#Here is code to maximum length of subarray consisting of same type of element on both halves of sub and the other steps required.
 def maxLengthSubArray ( A , N ) :
	forward = [ 0 ] * N
	backward = [ 0 ] * N
	for i in range ( N ) :
		if i == 0 or A [ i ] != A [ i - 1 ] :
			forward [ i ] = 1
		else :
			forward [ i ] = forward [ i - 1 ] + 1
	for i in range ( N - 1 , - 1 , - 1 ) :
		if i == N - 1 or A [ i ] != A [ i + 1 ] :
			backward [ i ] = 1
		else :
			backward [ i ] = backward [ i + 1 ] + 1
	ans = 0
	for i in range ( N - 1 ) :
		if ( A [ i ] != A [ i + 1 ] ) :
			ans = max ( ans , min ( forward [ i ] , backward [ i + 1 ] ) * 2 ) ;
	print ( ans )
arr = [ 1 , 2 , 3 , 4 , 4 , 4 , 6 , 6 , 6 , 9 ]
N = len ( arr )
maxLengthSubArray ( arr , N )