human
Length of the longest ZigZag subarray of the given array | Function to find the length of longest zigZag contiguous subarray.
' _ max ' to store the length of longest zigZag subarray.
' _ len ' to store the lengths of longest zigZag subarray at different instants of time.
Traverse the array from the beginning.
Check if ' _ max ' length is less than the length of the current zigzag subarray . If true , then update '_max.
Reset ' _ len ' to 1 as from this element , again the length of the new zigzag subarray is being calculated.
Comparing the length of the last zigzag subarray with '_max.
Return required maximum length.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to length of the longest ZigZag subarray of the given array and the other steps required.
 def lenOfLongZigZagArr ( a , n ) :
	_max = 1
	_len = 1
	for i in range ( n - 1 ) :
		if i % 2 == 0 and a [ i ] < a [ i + 1 ] :
			_len += 1
		elif i % 2 == 1 and a [ i ] > a [ i + 1 ] :
			_len += 1
		else :
'
	if _max < _len :
		_max = _len
	_len = 1
'
	if _max < _len :
		_max = _len
	return _max
arr = [ 1 , 2 , 3 , 4 , 5 ]
n = len ( arr )
print ( lenOfLongZigZagArr ( arr , n ) )