human
Write me a code to maximum length sub and the other steps required given the following:
Maximum length sub | Function to return the maximum length of the required sub - array.
To store the maximum length for a valid subarray.
To store the count of contiguous similar elements for previous group and the current group.
If current element is equal to the previous element then it is a part of the same group.
Else update the previous group and start counting elements for the new group.
Update the maximum possible length for a group.
Return the maximum length of the valid subarray.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to maximum length sub and the other steps required.
 def maxLength ( a , n ) :
	maxLen = 0 ;
	prev_cnt = 0 ; curr_cnt = 1 ;
	for i in range ( 1 , n ) :
		if ( a [ i ] == a [ i - 1 ] ) :
			curr_cnt += 1 ;
		else :
			prev_cnt = curr_cnt ;
			curr_cnt = 1 ;
		maxLen = max ( maxLen , min ( prev_cnt , curr_cnt ) ) ;
	return ( 2 * maxLen ) ;
arr = [ 1 , 1 , 1 , 0 , 0 , 1 , 1 ] ;
n = len ( arr ) ;
print ( maxLength ( arr , n ) ) ;