Maximum count number of valley elements in a subarray of size K | Function to find the valley elements in the array which contains in the subarrays of the size K ; Increment min_point if element at index i is smaller than element at index i + 1 and i - 1 ; final_point to maintain maximum of min points of subarray ; Iterate over array from kth element ; Leftmost element of subarray ; Rightmost element of subarray ; if new subarray have greater number of min points than previous subarray , then final_point is modified ; Max minimum points in subarray of size k ; Driver Code
def minpoint ( arr , n , k ) : NEW_LINE INDENT min_point = 0 NEW_LINE for i in range ( 1 , k - 1 ) : NEW_LINE INDENT if ( arr [ i ] < arr [ i - 1 ] and arr [ i ] < arr [ i + 1 ] ) : NEW_LINE INDENT min_point += 1 NEW_LINE DEDENT DEDENT final_point = min_point NEW_LINE for i in range ( k , n ) : NEW_LINE INDENT if ( arr [ i - ( k - 1 ) ] < arr [ i - ( k - 1 ) + 1 ] and arr [ i - ( k - 1 ) ] < arr [ i - ( k - 1 ) - 1 ] ) : NEW_LINE INDENT min_point -= 1 NEW_LINE DEDENT if ( arr [ i - 1 ] < arr [ i ] and arr [ i - 1 ] < arr [ i - 2 ] ) : NEW_LINE INDENT min_point += 1 NEW_LINE DEDENT if ( min_point > final_point ) : NEW_LINE INDENT final_point = min_point NEW_LINE DEDENT DEDENT print ( final_point ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 1 , 4 , 2 , 3 , 4 , 1 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE k = 4 NEW_LINE minpoint ( arr , n , k ) NEW_LINE DEDENT