human
Min difference between maximum and minimum element in all Y size subarrays | Function to get the maximum of all the subarrays of size Y.
ith index of maxarr array will be the index upto which Arr [ i ] is maximum.
Stack is used to find the next larger element and keeps track of index of current iteration.
Loop for remaining indexes.
j < i used to keep track whether jth element is inside or outside the window.
Return submax.
Function to get the minimum for all subarrays of size Y.
ith index of minarr array will be the index upto which Arr [ i ] is minimum.
Stack is used to find the next smaller element and keeping track of index of current iteration.
Loop for remaining indexes.
j < i used to keep track whether jth element is inside or outside the window.
Return submin.
Function to get minimum difference.
Create submin and submax arrays.
Store initial difference.
Calculate temporary difference.
Final minimum difference.
Given array arr [ ].
Given subarray size.
Function call"
Solution in Python:
assistant
#Here is code to min difference between maximum and minimum element in all Y size subarrays and the other steps required.
 def get_submaxarr ( arr , n , y ) :
	j = 0
	stk = [ ]
	maxarr = [ 0 ] * n
	stk . append ( 0 )
	for i in range ( 1 , n ) :
		while ( len ( stk ) > 0 and arr [ i ] > arr [ stk [ - 1 ] ] ) :
			maxarr [ stk [ - 1 ] ] = i - 1
			stk . pop ( )
		stk . append ( i )
	while ( stk ) :
		maxarr [ stk [ - 1 ] ] = n - 1
		stk . pop ( )
	submax = [ ]
	for i in range ( n - y + 1 ) :
		while ( maxarr [ j ] < i + y - 1 or j < i ) :
			j += 1
		submax . append ( arr [ j ] )
	return submax
def get_subminarr ( arr , n , y ) :
	j = 0
	stk = [ ]
	minarr = [ 0 ] * n
	stk . append ( 0 )
	for i in range ( 1 , n ) :
		while ( stk and arr [ i ] < arr [ stk [ - 1 ] ] ) :
			minarr [ stk [ - 1 ] ] = i
			stk . pop ( )
		stk . append ( i )
	while ( stk ) :
		minarr [ stk [ - 1 ] ] = n
		stk . pop ( )
	submin = [ ]
	for i in range ( n - y + 1 ) :
		while ( minarr [ j ] <= i + y - 1 or j < i ) :
			j += 1
		submin . append ( arr [ j ] )
	return submin
def getMinDifference ( Arr , N , Y ) :
	submin = get_subminarr ( Arr , N , Y )
	submax = get_submaxarr ( Arr , N , Y )
	minn = submax [ 0 ] - submin [ 0 ]
	b = len ( submax )
	for i in range ( 1 , b ) :
		diff = submax [ i ] - submin [ i ]
		minn = min ( minn , diff )
	print ( minn )
arr = [ 1 , 2 , 3 , 3 , 2 , 2 ]
N = len ( arr )
Y = 4
getMinDifference ( arr , N , Y )