Find if a crest is present in the index range [ L , R ] of the given array | Function that returns true if the array contains a crest in the index range [ L , R ] ; To keep track of elements which satisfy the Property ; Property is satisfied for the current element ; Cumulative Sum ; If a crest is present in the given index range ; Driver code
def hasCrest ( arr , n , L , R ) : NEW_LINE INDENT present = [ 0 ] * n ; NEW_LINE for i in range ( 1 , n - 2 + 1 ) : NEW_LINE INDENT if ( ( arr [ i ] <= arr [ i + 1 ] ) and ( arr [ i ] <= arr [ i - 1 ] ) ) : NEW_LINE INDENT present [ i ] = 1 ; NEW_LINE DEDENT DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT present [ i ] += present [ i - 1 ] ; NEW_LINE DEDENT if ( present [ L ] == present [ R - 1 ] ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 1 , 3 , 5 , 12 , 11 , 7 , 9 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE L = 2 ; NEW_LINE R = 6 ; NEW_LINE if ( hasCrest ( arr , N , L , R ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT