human
Longest subarray with all even or all odd elements | Function to calculate longest substring with odd or even elements.
Initializing dp [ ].
Initializing dp [ 0 ] with 1.
ans will store the final answer.
Traversing the array from index 1 to N - 1.
Checking both current and previous element is even or odd.
Updating dp [ i ] with dp [ i - 1 ] + 1.
Storing max element to ans.
Returning the final answer.
Driver Code.
Input.
Function call"
Solution in Python:
assistant
#Here is code to longest subarray with all even or all odd elements and the other steps required.
 def LongestOddEvenSubarray ( A , N ) :
	dp = [ 0 for i in range ( N ) ]
	dp [ 0 ] = 1
	ans = 1
	for i in range ( 1 , N , 1 ) :
		if ( ( A [ i ] % 2 == 0 and A [ i - 1 ] % 2 == 0 ) or ( A [ i ] % 2 != 0 and A [ i - 1 ] % 2 != 0 ) ) :
			dp [ i ] = dp [ i - 1 ] + 1
		else :
			dp [ i ] = 1
	for i in range ( N ) :
		ans = max ( ans , dp [ i ] )
	return ans
if __name__ == ' _ _ main _ _ ' :
	A = [ 2 , 5 , 7 , 2 , 4 , 6 , 8 , 3 ]
	N = len ( A )
	print ( LongestOddEvenSubarray ( A , N ) )