Check if a triplet of buildings can be selected such that the third building is taller than the first building and smaller than the second building | Function to check if it is possible to select three buildings that satisfy the given condition ; Stores prefix min array ; Iterate over the range [ 1 , N - 1 ] ; Stores the element from the ending in increasing order ; Iterate until j is greater than or equal to 0 ; If current array element is greater than the prefix min upto j ; Iterate while stack is not empty and top element is less than or equal to preMin [ j ] ; Remove the top element ; If stack is not empty and top element of the stack is less than the current element ; Push the arr [ j ] in stack ; If none of the above case satisfy then return " No " ; Driver code ; Input
def recreationalSpot ( arr , N ) : NEW_LINE INDENT if ( N < 3 ) : NEW_LINE INDENT return " No " NEW_LINE DEDENT preMin = [ 0 ] * N NEW_LINE preMin [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT preMin [ i ] = min ( preMin [ i - 1 ] , arr [ i ] ) NEW_LINE DEDENT stack = [ ] NEW_LINE for j in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ j ] > preMin [ j ] ) : NEW_LINE INDENT while ( len ( stack ) > 0 and stack [ - 1 ] <= preMin [ j ] ) : NEW_LINE INDENT del stack [ - 1 ] NEW_LINE DEDENT if ( len ( stack ) > 0 and stack [ - 1 ] < arr [ j ] ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT stack . append ( arr [ j ] ) NEW_LINE DEDENT DEDENT return " No " NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 7 , 11 , 5 , 13 , 2 ] NEW_LINE size = len ( arr ) NEW_LINE print ( recreationalSpot ( arr , size ) ) NEW_LINE DEDENT