Find a triplet in an array such that arr [ i ] arr [ k ] and i < j < k | Java program to implement the above approach ; 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 ; Print a triplet ; If no triplet found ; Driver Code
import java . util . * ; class GFG { static void FindTrip ( int arr [ ] , int N ) { for ( int i = 1 ; i < N - 1 ; i ++ ) { int p = arr [ i - 1 ] ; int q = arr [ i ] ; int r = arr [ i + 1 ] ; if ( p < q && q > r ) { System . out . println ( ( i - 1 ) + " ▁ " + ( i ) + " ▁ " + ( i + 1 ) ) ; return ; } } System . out . println ( - 1 ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 1 , 4 , 3 } ; int N = arr . length ; FindTrip ( arr , N ) ; } }