Find a triplet in an array such that arr [ i ] arr [ k ] and i < j < k | Function to find a triplet that satisfy the conditions ; Traverse the given array ; Stores current element ; Stores element just before the current element ; Stores element just after the current element ; Check the given conditions ; Prlet a triplet ; If no triplet found ; Driver Code
function FindTrip ( arr , N ) { for ( let i = 1 ; i < N - 1 ; i ++ ) { let p = arr [ i - 1 ] ; let q = arr [ i ] ; let r = arr [ i + 1 ] ; if ( p < q && q > r ) { document . write ( ( i - 1 ) + " " + ( i ) + " " + ( i + 1 ) ) ; return ; } } document . write ( - 1 ) ; } let arr = [ 2 , 1 , 4 , 3 ] ; let N = arr . length ; FindTrip ( arr , N ) ;