Pair having all other given pairs lying between its minimum and maximum | Java program for the above approach ; Function to find the position of the pair that covers every pair in the array arr [ ] [ ] ; Position to store the index ; Stores the maximum second value ; Stores the minimum first value ; Iterate over the array of pairs ; Update right maximum ; Update left minimum ; Iterate over the array of pairs ; If any pair exists with value { left , right } then store it ; Print the answer ; Driver Code ; Given array of pairs ; Function Call
import java . util . * ; class GFG { static void position ( int arr [ ] [ ] , int N ) { int pos = - 1 ; int right = Integer . MIN_VALUE ; int left = Integer . MAX_VALUE ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] [ 1 ] > right ) { right = arr [ i ] [ 1 ] ; } if ( arr [ i ] [ 0 ] < left ) { left = arr [ i ] [ 0 ] ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] [ 0 ] == left && arr [ i ] [ 1 ] == right ) { pos = i + 1 ; } } System . out . print ( pos + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 3 , 3 } , { 1 , 3 } , { 2 , 2 } , { 2 , 3 } , { 1 , 2 } } ; int N = arr . length ; position ( arr , N ) ; } }