human
Longest subarray such that the difference of max and min is at | Python 3 code to find longest subarray with difference between max and min as at - most 1..
longest constant range length.
first number.
If we see same number.
If we see different number , but same as previous ..
If number is neither same as previous nor as current ..
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to longest subarray such that the difference of max and min is at and the other steps required.
 def longestSubarray ( input , length ) :
	prev = - 1
	prevCount = 0
	currentCount = 1
	longest = 1
	current = input [ 0 ]
	for i in range ( 1 , length ) :
		next = input [ i ]
		if next == current :
			currentCount += 1
		elif next == prev :
			prevCount += currentCount
			prev = current
			current = next
			currentCount = 1
		else :
			longest = max ( longest , currentCount + prevCount )
			prev = current
			prevCount = currentCount
			current = next
			currentCount = 1
	return max ( longest , currentCount + prevCount )
if __name__ == " _ _ main _ _ " :
	input = [ 5 , 5 , 6 , 7 , 6 ]
	n = len ( input )
	print ( longestSubarray ( input , n ) )