human
Maximum length sub | Function to return the maximum length of the required sub - array.
For the first consecutive pair of elements.
While a consecutive pair can be selected.
If current pair forms a valid sub - array.
2 is the length of the current sub - array.
To extend the sub - array both ways.
While elements at indices l and r are part of a valid sub - array.
Update the maximum length so far.
Select the next consecutive pair.
Return the maximum length.
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 ( arr , n ) :
	maxLen = 0
	i = 0
	j = i + 1
	while ( j < n ) :
		if ( arr [ i ] != arr [ j ] ) :
			maxLen = max ( maxLen , 2 )
			l = i - 1
			r = j + 1
			while ( l >= 0 and r < n and arr [ l ] == arr [ i ] and arr [ r ] == arr [ j ] ) :
				l -= 1
				r += 1
			maxLen = max ( maxLen , 2 * ( r - j ) )
		i += 1
		j = i + 1
	return maxLen
arr = [ 1 , 1 , 1 , 0 , 0 , 1 , 1 ]
n = len ( arr )
print ( maxLength ( arr , n ) )