Check if a point having maximum X and Y coordinates exists or not | C ++ program for the above approach ; Initialize INF as inifnity ; Function to return the point having maximum X and Y coordinates ; Base Case ; Stores if valid point exists ; If point arr [ i ] is valid ; Check for the same point ; Check for a valid point ; If current point is the required point ; Otherwise ; Function to find the required point ; Stores the point with maximum X and Y - coordinates ; If no required point exists ; Driver Code ; Given array of points ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int INF = INT_MAX ; int * findMaxPoint ( int arr [ ] [ 2 ] , int i , int n ) { if ( i == n ) { arr [ 0 ] [ 0 ] = INF ; arr [ 0 ] [ 1 ] = INF ; return arr [ 0 ] ; } bool flag = true ; for ( int j = 0 ; j < n ; j ++ ) { if ( j == i ) continue ; if ( arr [ j ] [ 0 ] >= arr [ i ] [ 0 ] arr [ j ] [ 1 ] >= arr [ i ] [ 1 ] ) { flag = false ; break ; } } if ( flag ) return arr [ i ] ; return findMaxPoint ( arr , i + 1 , n ) ; } void findMaxPoints ( int arr [ ] [ 2 ] , int n ) { int ans [ 2 ] ; memcpy ( ans , findMaxPoint ( arr , 0 , n ) , 2 * sizeof ( int ) ) ; if ( ans [ 0 ] == INF ) { cout << -1 ; } else { cout << " ( " << ans [ 0 ] << " ▁ " << ans [ 1 ] << " ) " ; } } int main ( ) { int arr [ ] [ 2 ] = { { 1 , 2 } , { 2 , 1 } , { 3 , 4 } , { 4 , 3 } , { 5 , 5 } } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findMaxPoints ( arr , N ) ; return 0 ; }