Check whether there exists a triplet ( i , j , k ) such that arr [ i ] < arr [ k ] < arr [ j ] for i < j < k | 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 h2 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 ; Given array ; Function Call
function findTriplet ( arr ) { var n = arr . length ; var st = [ ] ; var h3 = - 1000000000 , h1 = 1000000000 ; for ( var i = n - 1 ; i >= 0 ; i -- ) { h1 = arr [ i ] ; while ( st . length != 0 && st [ st . length - 1 ] < arr [ i ] ) { h3 = st [ st . length - 1 ] ; st . pop ( ) ; } st . push ( arr [ i ] ) ; if ( h1 < h3 ) { return true ; } } return false ; } var arr = [ 4 , 7 , 5 , 6 ] ; if ( findTriplet ( arr ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }