Find subarray of Length K with Maximum Peak | Function to find the subarray ; Make prefix array to store the prefix sum of peak count ; Count peak for previous index ; Check if this element is a peak ; Increment the count ; Check if number of peak in the sub array whose l = i is greater or not ; Print the result ; Driver code
def findSubArray ( a , n , k ) : NEW_LINE INDENT pref = [ 0 for i in range ( n ) ] NEW_LINE pref [ 0 ] = 0 NEW_LINE for i in range ( 1 , n - 1 , 1 ) : NEW_LINE INDENT pref [ i ] = pref [ i - 1 ] NEW_LINE if ( a [ i ] > a [ i - 1 ] and a [ i ] > a [ i + 1 ] ) : NEW_LINE INDENT pref [ i ] += 1 NEW_LINE DEDENT DEDENT peak = 0 NEW_LINE left = 0 NEW_LINE for i in range ( 0 , n - k + 1 , 1 ) : NEW_LINE INDENT if ( pref [ i + k - 2 ] - pref [ i ] > peak ) : NEW_LINE INDENT peak = pref [ i + k - 2 ] - pref [ i ] NEW_LINE left = i NEW_LINE DEDENT DEDENT print ( " Left ▁ = " , left + 1 ) NEW_LINE print ( " Right ▁ = " , left + k ) NEW_LINE print ( " Peak ▁ = " , peak ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , 3 , 2 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE findSubArray ( arr , n , k ) NEW_LINE DEDENT