Min and max length subarray having adjacent element difference atmost K | Function to find the maximum and minimum length subarray ; Initialise minimum and maximum size of subarray in worst case ; Left will scan the size of possible subarray in left of selected element ; Right will scan the size of possible subarray in right of selected element ; Temp will store size of group associateed with every element ; Loop to find size of subarray for every element of array ; Left will move in left direction and compare difference between itself and element left to it ; Right will move in right direction and compare difference between itself and element right to it ; If subarray of much lesser or much greater is found than yet known then update ; Print minimum and maximum possible size of subarray ; Driver Code ; Function call to find maximum and minimum possible sub array
def findMaxMinSubArray ( arr , K , n ) : NEW_LINE INDENT min = n NEW_LINE max = 0 NEW_LINE left = 0 NEW_LINE right = n NEW_LINE tmp = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT tmp = 1 NEW_LINE left = i NEW_LINE while ( left - 1 >= 0 and abs ( arr [ left ] - arr [ left - 1 ] ) <= K ) : NEW_LINE INDENT left = left - 1 NEW_LINE tmp = tmp + 1 NEW_LINE DEDENT right = i NEW_LINE while ( right + 1 <= n - 1 and abs ( arr [ right ] - arr [ right + 1 ] ) <= K ) : NEW_LINE INDENT right = right + 1 NEW_LINE tmp = tmp + 1 NEW_LINE DEDENT if ( min > tmp ) : NEW_LINE INDENT min = tmp NEW_LINE DEDENT if ( max < tmp ) : NEW_LINE INDENT max = tmp NEW_LINE DEDENT DEDENT print ( min , end = ' , ▁ ' ) NEW_LINE print ( max , end =   ' ' ) NEW_LINE DEDENT arr = [ 1 , 2 , 5 , 6 , 7 ] NEW_LINE K = 2 NEW_LINE n = len ( arr ) NEW_LINE findMaxMinSubArray ( arr , K , n ) NEW_LINE