Check whether there exists a triplet ( i , j , k ) such that arr [ i ] < arr [ k ] < arr [ j ] for i < j < k | Python3 program for the above approach ; Function to check if there exist triplet in the array such that i < j < k and arr [ i ] < arr [ k ] < arr [ j ] ; Initialize the heights of h1 and h3 to INT_MAX and INT_MIN respectively ; Store the current element as h1 ; If the element at top of stack is less than the current element then pop the stack top and keep updating the value of h3 ; Push the current element on the stack ; If current element is less than h3 , then we found such triplet and return true ; No triplet found , hence return false ; Driver Code ; Given array ; Function Call
import sys NEW_LINE def findTriplet ( arr ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE st = [ ] NEW_LINE h3 = - sys . maxsize - 1 NEW_LINE h1 = sys . maxsize NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT h1 = arr [ i ] NEW_LINE while ( len ( st ) > 0 and st [ - 1 ] < arr [ i ] ) : NEW_LINE INDENT h3 = st [ - 1 ] NEW_LINE del st [ - 1 ] NEW_LINE DEDENT st . append ( arr [ i ] ) NEW_LINE if ( h1 < h3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 7 , 5 , 6 ] NEW_LINE if ( findTriplet ( arr ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT