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:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
 
import java.io.*;
 
class GFG {
 
    // Initialize INF as inifnity
    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);
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using System;
 
class GFG{
 
// Initialize INF as inifnity
static int INF = int.MaxValue;
 
// 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
    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 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)
    {
        Console.WriteLine(-1);
    }
    else
    {
        Console.WriteLine("(" + 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.GetLength(0);
 
    // Function Call
    findMaxPoints(arr, N);
}
}
 
// This code is contributed by Princi Singh

chevron_right


Output: 

(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:

  1. Traverse the given array arr[] and find the maximum X and Y coordinates. Let them be maxX and maxY.
  2. Again traverse the array arr[] from i = 0 to N-1 checking if (arr[i].X, arr[i].Y) is equals to (maxX, maxY).
  3. If the (maxX, maxY) is present in the array arr[], print (maxX, maxY) else print -1.

Below is the implementation of the above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the point having
// max X and Y coordinates
static 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 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);
}
}
 
// This code is contributed by Amit Katiyar

chevron_right


Output: 

(5, 5)






 

Time Complexity: O(N)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.