Check if a point having maximum X and Y coordinates exists or not

Given a 2D array arr[] consisting of N coordinates of the form (X, Y), the task is to find a coordinate from the given array such that the X-coordinate of this point is greater than all other X-coordinates and the Y-coordinate of this point is greater than all other Y-coordinates. If no such point exists, print -1.
Examples:
Input: arr[][] = {(1, 2), (2, 1), (3, 4), (4, 3), (5, 5)}
Output: (5, 5)
Explanation:
The maximum X-coordinate is 5 and the maximum Y-coordinate is 5.
Since the point (5, 5) is present in the array, print (5, 5) as the required answer.Input: arr[] = {(5, 3), (3, 5)}
Output: -1
Explanation:
The maximum X-coordinate is 5 and maximum Y-coordinate is 5. Since+ (5, 5) is not present. Therefore, print -1.Â
Naive Approach: The simplest approach is to traverse the array and for each point, check if it is the maximum X and Y-coordinates or not. If no such point exists, print -1. Otherwise, print the point as the required answer.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;Â
// Initialize INF as infinityint INF = INT_MAX;Â
// Function to return the point having// maximum X and Y coordinatesint* findMaxPoint(int arr[][2], int i, int n){         // Base Case    if (i == n)     {        arr[0][0] = INF;        arr[0][1] = INF;        return arr[0];    }Â
    // Stores if valid point exists    bool flag = true;Â
    // If point arr[i] is valid    for(int j = 0; j < n; j++)    {                 // Check for the same point        if (j == i)            continue;Â
        // Check for a valid point        if (arr[j][0] >= arr[i][0] ||             arr[j][1] >= arr[i][1])         {            flag = false;            break;        }    }Â
    // If current point is the    // required point    if (flag)        return arr[i];Â
    // Otherwise    return findMaxPoint(arr, i + 1, n);}Â
// Function to find the required pointvoid findMaxPoints(int arr[][2], int n){         // Stores the point with maximum    // X and Y-coordinates    int ans[2];    memcpy(ans, findMaxPoint(arr, 0, n),           2 * sizeof(int));Â
    // If no required point exists    if (ans[0] == INF)     {        cout << -1;    }    else    {        cout << "(" << ans[0] << " "             << ans[1] << ")";    }}Â
// Driver Codeint main(){         // Given array of points    int arr[][2] = { { 1, 2 }, { 2, 1 },                     { 3, 4 }, { 4, 3 },                     { 5, 5 } };Â
    int N = sizeof(arr) / sizeof(arr[0]);Â
    // Function Call    findMaxPoints(arr, N);         return 0;}Â
// This code is contributed by subhammahato348 |
Java
// Java program for the above approachÂ
import java.io.*;Â
class GFG {Â
    // Initialize INF as infinity    static int INF = Integer.MAX_VALUE;Â
    // Function to return the point having    // maximum X and Y coordinates    static int[] findMaxPoint(        int arr[][], int i, int n)    {        // Base Case        if (i == n)            return new int[] { INF, INF };Â
        // Stores if valid point exists        boolean flag = true;Â
        // If point arr[i] is valid        for (int j = 0; j < n; j++) {Â
            // Check for the same point            if (j == i)                continue;Â
            // Check for a valid point            if (arr[j][0] >= arr[i][0]                || arr[j][1] >= arr[i][1]) {                flag = false;                break;            }        }Â
        // If current point is the        // required point        if (flag)            return arr[i];Â
        // Otherwise        return findMaxPoint(arr, i + 1, n);    }Â
    // Function to find the required point    static void findMaxPoints(int arr[][],                              int n)    {        // Stores the point with maximum        // X and Y-coordinates        int ans[] = findMaxPoint(arr, 0, n);Â
        // If no required point exists        if (ans[0] == INF) {            System.out.println(-1);        }        else {            System.out.println(                "(" + ans[0] + " "                + ans[1] + ")");        }    }Â
    // Driver Code    public static void main(String[] args)    {        // Given array of points        int arr[][] = new int[][] {{ 1, 2 }, { 2, 1 },                                    { 3, 4 }, { 4, 3 },                                    { 5, 5 }};Â
        int N = arr.length;Â
        // Function Call        findMaxPoints(arr, N);    }} |
Python3
# Python3 program for the above approachimport sysÂ
# Initialize INF as infinityINF = sys.maxsize;Â
# Function to return the point having# maximum X and Y coordinatesdef findMaxPoint(arr, i, n):       # Base Case    if (i == n):        return [INF, INF]Â
    # Stores if valid point exists    flag = True;Â
    # If point arr[i] is valid    for j in range(n):Â
        # Check for the same point        if (j == i):            continue;Â
        # Check for a valid point        if (arr[j][0] >= arr[i][0] or arr[j][1] >= arr[i][1]):            flag = False;            break;Â
    # If current point is the    # required point    if (flag):        return arr[i];Â
    # Otherwise    return findMaxPoint(arr, i + 1, n);Â
# Function to find the required pointdef findMaxPoints(arr, n):       # Stores the point with maximum    # X and Y-coordinates    ans = findMaxPoint(arr, 0, n);Â
    # If no required point exists    if (ans[0] == INF):        print(-1);    else:        print("(" , ans[0] , " " , ans[1] , ")");Â
# Driver Codeif __name__ == '__main__':       # Given array of points    arr = [[1, 2],           [2, 1],           [3, 4],           [4, 3],           [5, 5]];Â
    N = len(arr);Â
    # Function Call    findMaxPoints(arr, N);Â
# This code is contributed by shikhasingrajput |
C#
// C# program for the above approachusing System;Â
class GFG{Â
// Initialize INF as infinitystatic int INF = int.MaxValue;Â
// Function to return the point having// maximum X and Y coordinatesstatic int[] findMaxPoint(int [,]arr, int i,                          int n){         // Base Case    if (i == n)        return new int[]{INF, INF};Â
    // Stores if valid point exists    bool flag = true;Â
    // If point arr[i] is valid    for(int j = 0; j < n; j++)    {                 // Check for the same point        if (j == i)            continue;Â
        // Check for a valid point        if (arr[j, 0] >= arr[i, 0] ||             arr[j, 1] >= arr[i, 1])        {            flag = false;            break;        }    }Â
    // If current point is the    // required point    int []ans = new int[arr.GetLength(1)];    if (flag)    {        for(int k = 0; k < ans.GetLength(0); k++)            ans[k] = arr[i, k];                     return ans;    }Â
    // Otherwise    return findMaxPoint(arr, i + 1, n);}Â
// Function to find the required pointstatic void findMaxPoints(int [,]arr,                          int n){         // Stores the point with maximum    // X and Y-coordinates    int []ans = findMaxPoint(arr, 0, n);Â
    // If no required point exists    if (ans[0] == INF)     {        Console.WriteLine(-1);    }    else    {        Console.WriteLine("(" + ans[0] + " " +                                 ans[1] + ")");    }}Â
// Driver Codepublic static void Main(String[] args){         // Given array of points    int [,]arr = new int[,]{ { 1, 2 }, { 2, 1 },                              { 3, 4 }, { 4, 3 },                              { 5, 5 } };Â
    int N = arr.GetLength(0);Â
    // Function Call    findMaxPoints(arr, N);}}Â
// This code is contributed by Princi Singh |
Javascript
<script>Â
// Javascript program for the above approachÂ
// Initialize INF as infinitylet INF = Number.MAX_VALUE;Â
// Function to return the point having// maximum X and Y coordinatesfunction findMaxPoint(arr, i, n){         // Base Case    if (i == n)        return [INF, INF];Â
    // Stores if valid point exists    let flag = true;Â
    // If point arr[i] is valid    for(let j = 0; j < n; j++)     {                 // Check for the same point        if (j == i)            continue;Â
        // Check for a valid point        if (arr[j][0] >= arr[i][0] ||             arr[j][1] >= arr[i][1])        {            flag = false;            break;        }    }Â
    // If current point is the    // required point    if (flag)        return arr[i];Â
    // Otherwise    return findMaxPoint(arr, i + 1, n);}Â
// Function to find the required pointfunction findMaxPoints(arr, n){         // Stores the point with maximum    // X and Y-coordinates    let ans = findMaxPoint(arr, 0, n);Â
    // If no required point exists    if (ans[0] == INF)     {        document.write(-1);    }    else    {        document.write("(" + ans[0] + " " +                              ans[1] + ")");    }}Â
// Driver codeÂ
// Given array of pointslet arr = [ [ 1, 2 ], [ 2, 1 ],            [ 3, 4 ], [ 4, 3 ],            [ 5, 5 ] ];Â
let N = arr.length;Â
// Function CallfindMaxPoints(arr, N);Â
// This code is contributed by splevel62Â
</script> |
(5 5)
Â
Time Complexity: O(N2) where N is the length of the given array.
Auxiliary Space: O(N)
Efficient Approach: The idea is to find the maximum X and Y coordinates. Let them be maxX and maxY. Again traverse the given array checking if the point (maxX, maxY) is present. Follow the below steps to solve the problem:
- Traverse the given array arr[] and find the maximum X and Y coordinates. Let them be maxX and maxY.
- Again traverse the array arr[] from i = 0 to N-1 checking if (arr[i].X, arr[i].Y) is equals to (maxX, maxY).
- If the (maxX, maxY) is present in the array arr[], print (maxX, maxY) else print -1.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;#define N 5#define P 2Â
// Function to find the point having// max X and Y coordinatesvoid findMaxPoint(int arr[N][P]){Â Â Â Â Â Â Â Â Â // Initialize maxX and maxYÂ Â Â Â int maxX = INT_MIN;Â Â Â Â int maxY = INT_MIN;Â
    // Length of the given array    int n = N;Â
    // Get maximum X & Y coordinates    for(int i = 0; i < n; i++)    {        maxX = max(maxX, arr[i][0]);        maxY = max(maxY, arr[i][1]);    }Â
    // Check if the required point    // i.e., (maxX, maxY) is present    for(int i = 0; i < n; i++)    {                 // If point with maximum X and        // Y coordinates is present        if (maxX == arr[i][0] &&            maxY == arr[i][1])         {            cout << "(" << maxX << ", "                        << maxY << ")";                              return;        }    }Â
    // If no such point exists    cout << (-1);}Â
// Driver Codeint main(){         // Given array of points    int arr[N][P] = { { 1, 2 }, { 2, 1 },                      { 3, 4 }, { 4, 3 },                       { 5, 5 } };Â
    // Print answer    findMaxPoint(arr);}Â
// This code is contributed by 29AjayKumar |
Java
// Java program for the above approachÂ
import java.io.*;Â
class GFG {Â
    // Function to find the point having    // max X and Y coordinates    static void findMaxPoint(int arr[][])    {        // Initialize maxX and maxY        int maxX = Integer.MIN_VALUE;        int maxY = Integer.MIN_VALUE;Â
        // Length of the given array        int n = arr.length;Â
        // Get maximum X & Y coordinates        for (int i = 0; i < n; i++) {            maxX = Math.max(maxX, arr[i][0]);            maxY = Math.max(maxY, arr[i][1]);        }Â
        // Check if the required point        // i.e., (maxX, maxY) is present        for (int i = 0; i < n; i++) {Â
            // If point with maximum X and            // Y coordinates is present            if (maxX == arr[i][0]                && maxY == arr[i][1]) {Â
                System.out.println(                    "(" + maxX + ", "                    + maxY + ")");                return;            }        }Â
        // If no such point exists        System.out.println(-1);    }Â
    // Driver Code    public static void main(String[] args)    {        // Given array of points        int arr[][] = new int[][] {{ 1, 2 }, { 2, 1 },                                    { 3, 4 }, { 4, 3 },                                    { 5, 5 }};Â
        // Print answer        findMaxPoint(arr);    }} |
Python3
# Python3 program for the above approachimport sys;Â
# Function to find the pohaving# max X and Y coordinatesdef findMaxPoint(arr):Â Â Â Â Â Â Â Â Â # Initialize maxX and maxYÂ Â Â Â maxX = -sys.maxsize;Â Â Â Â maxY = -sys.maxsize;Â
    # Length of the given array    n = len(arr);Â
    # Get maximum X & Y coordinates    for i in range(n):        maxX = max(maxX, arr[i][0]);        maxY = max(maxY, arr[i][1]);Â
    # Check if the required point    # i.e., (maxX, maxY) is present    for i in range(n):Â
        # If powith maximum X and        # Y coordinates is present        if (maxX == arr[i][0] and maxY == arr[i][1]):            print("(" , maxX , ", " , maxY , ")");            return;Â
    # If no such poexists    print(-1);Â
# Driver Codeif __name__ == '__main__':       # Given array of points    arr = [[1, 2], [2, 1], [3, 4], [4, 3], [5, 5]];Â
    # Print answer    findMaxPoint(arr);Â
# This code is contributed by gauravrajput1 |
C#
// C# program for the above approachusing System;Â
class GFG{Â
// Function to find the point having// max X and Y coordinatesstatic void findMaxPoint(int [,]arr){Â Â Â Â Â Â Â Â Â // Initialize maxX and maxYÂ Â Â Â int maxX = int.MinValue;Â Â Â Â int maxY = int.MinValue;Â
    // Length of the given array    int n = arr.GetLength(0);Â
    // Get maximum X & Y coordinates    for(int i = 0; i < n; i++)     {        maxX = Math.Max(maxX, arr[i, 0]);        maxY = Math.Max(maxY, arr[i, 1]);    }Â
    // Check if the required point    // i.e., (maxX, maxY) is present    for(int i = 0; i < n; i++)     {                 // If point with maximum X and        // Y coordinates is present        if (maxX == arr[i, 0] &&             maxY == arr[i, 1])        {            Console.WriteLine("(" + maxX + ", " +                                     maxY + ")");            return;        }    }Â
    // If no such point exists    Console.WriteLine(-1);}Â
// Driver Codepublic static void Main(String[] args){         // Given array of points    int [,]arr = new int[,]{ { 1, 2 }, { 2, 1 },                              { 3, 4 }, { 4, 3 },                              { 5, 5 } };Â
    // Print answer    findMaxPoint(arr);}}Â
// This code is contributed by Amit Katiyar |
Javascript
<script>Â
// JavaScript program for the above approachÂ
// Function to find the pohaving// max X and Y coordinatesfunction findMaxPoint(arr){Â Â Â Â Â Â Â Â //Â Initialize maxX and maxYÂ Â Â let maxX = Number.MIN_VALUE;Â Â Â let maxY = Number.MIN_VALUE;Â
   // Length of the given array   let n = arr.length;Â
   // Get maximum X & Y coordinates    for(let i=0;i<n;i++){        maxX = Math.max(maxX, arr[i][0]);        maxY = Math.max(maxY, arr[i][1]);    }Â
   // Check if the required point   // i.e., (maxX, maxY) is present    for(let i=0;i<n;i++){Â
      //  If powith maximum X and      //  Y coordinates is present        if (maxX == arr[i][0] && maxY == arr[i][1]){            document.write("(" , maxX , ", " , maxY , ")");            return;        }   }   // If no such poexists    document.write(-1);}Â
// Driver Code   // Given array of pointslet arr = [[1, 2], [2, 1], [3, 4], [4, 3], [5, 5]];Â
//Â PranswerfindMaxPoint(arr);Â
// This code is contributed by shinjanpatra Â
</script> |
(5, 5)
Â
Time Complexity: O(N)
Auxiliary Space: O(N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



