Check whether there exists a triplet ( i , j , k ) such that arr [ i ] < arr [ k ] < arr [ j ] for i < j < k | Java 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 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 ; Driver code ; Given array ; Function call
import java . util . * ; class GFG { public static boolean findTriplet ( int [ ] arr ) { int n = arr . length ; Stack < Integer > st = new Stack < > ( ) ; int h3 = Integer . MIN_VALUE ; int h1 = Integer . MAX_VALUE ; for ( int i = n - 1 ; i >= 0 ; i -- ) { h1 = arr [ i ] ; while ( ! st . empty ( ) && st . peek ( ) < arr [ i ] ) { h3 = st . peek ( ) ; st . pop ( ) ; } st . push ( arr [ i ] ) ; if ( h1 < h3 ) { return true ; } } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 7 , 5 , 6 } ; if ( findTriplet ( arr ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }