Check if a point having maximum X and Y coordinates exists or not
Last Updated :
12 Jul, 2022
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++
#include <bits/stdc++.h>
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;
}
|
Java
import java.io.*;
class GFG {
static int INF = Integer.MAX_VALUE;
static int [] findMaxPoint(
int arr[][], int i, int n)
{
if (i == n)
return new int [] { INF, INF };
boolean 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);
}
static void findMaxPoints( int arr[][],
int n)
{
int ans[] = findMaxPoint(arr, 0 , n);
if (ans[ 0 ] == INF) {
System.out.println(- 1 );
}
else {
System.out.println(
"(" + ans[ 0 ] + " "
+ ans[ 1 ] + ")" );
}
}
public static void main(String[] args)
{
int arr[][] = new int [][] {{ 1 , 2 }, { 2 , 1 },
{ 3 , 4 }, { 4 , 3 },
{ 5 , 5 }};
int N = arr.length;
findMaxPoints(arr, N);
}
}
|
Python3
import sys
INF = sys.maxsize;
def findMaxPoint(arr, i, n):
if (i = = n):
return [INF, INF]
flag = True ;
for j in range (n):
if (j = = i):
continue ;
if (arr[j][ 0 ] > = arr[i][ 0 ] or arr[j][ 1 ] > = arr[i][ 1 ]):
flag = False ;
break ;
if (flag):
return arr[i];
return findMaxPoint(arr, i + 1 , n);
def findMaxPoints(arr, n):
ans = findMaxPoint(arr, 0 , n);
if (ans[ 0 ] = = INF):
print ( - 1 );
else :
print ( "(" , ans[ 0 ] , " " , ans[ 1 ] , ")" );
if __name__ = = '__main__' :
arr = [[ 1 , 2 ],
[ 2 , 1 ],
[ 3 , 4 ],
[ 4 , 3 ],
[ 5 , 5 ]];
N = len (arr);
findMaxPoints(arr, N);
|
C#
using System;
class GFG{
static int INF = int .MaxValue;
static int [] findMaxPoint( int [,]arr, int i,
int n)
{
if (i == n)
return new int []{INF, INF};
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 ;
}
}
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;
}
return findMaxPoint(arr, i + 1, n);
}
static void findMaxPoints( int [,]arr,
int n)
{
int []ans = findMaxPoint(arr, 0, n);
if (ans[0] == INF)
{
Console.WriteLine(-1);
}
else
{
Console.WriteLine( "(" + ans[0] + " " +
ans[1] + ")" );
}
}
public static void Main(String[] args)
{
int [,]arr = new int [,]{ { 1, 2 }, { 2, 1 },
{ 3, 4 }, { 4, 3 },
{ 5, 5 } };
int N = arr.GetLength(0);
findMaxPoints(arr, N);
}
}
|
Javascript
<script>
let INF = Number.MAX_VALUE;
function findMaxPoint(arr, i, n)
{
if (i == n)
return [INF, INF];
let flag = true ;
for (let 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);
}
function findMaxPoints(arr, n)
{
let ans = findMaxPoint(arr, 0, n);
if (ans[0] == INF)
{
document.write(-1);
}
else
{
document.write( "(" + ans[0] + " " +
ans[1] + ")" );
}
}
let arr = [ [ 1, 2 ], [ 2, 1 ],
[ 3, 4 ], [ 4, 3 ],
[ 5, 5 ] ];
let N = arr.length;
findMaxPoints(arr, N);
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
#define N 5
#define P 2
void findMaxPoint( int arr[N][P])
{
int maxX = INT_MIN;
int maxY = INT_MIN;
int n = N;
for ( int i = 0; i < n; i++)
{
maxX = max(maxX, arr[i][0]);
maxY = max(maxY, arr[i][1]);
}
for ( int i = 0; i < n; i++)
{
if (maxX == arr[i][0] &&
maxY == arr[i][1])
{
cout << "(" << maxX << ", "
<< maxY << ")" ;
return ;
}
}
cout << (-1);
}
int main()
{
int arr[N][P] = { { 1, 2 }, { 2, 1 },
{ 3, 4 }, { 4, 3 },
{ 5, 5 } };
findMaxPoint(arr);
}
|
Java
import java.io.*;
class GFG {
static void findMaxPoint( int arr[][])
{
int maxX = Integer.MIN_VALUE;
int maxY = Integer.MIN_VALUE;
int n = arr.length;
for ( int i = 0 ; i < n; i++) {
maxX = Math.max(maxX, arr[i][ 0 ]);
maxY = Math.max(maxY, arr[i][ 1 ]);
}
for ( int i = 0 ; i < n; i++) {
if (maxX == arr[i][ 0 ]
&& maxY == arr[i][ 1 ]) {
System.out.println(
"(" + maxX + ", "
+ maxY + ")" );
return ;
}
}
System.out.println(- 1 );
}
public static void main(String[] args)
{
int arr[][] = new int [][] {{ 1 , 2 }, { 2 , 1 },
{ 3 , 4 }, { 4 , 3 },
{ 5 , 5 }};
findMaxPoint(arr);
}
}
|
Python3
import sys;
def findMaxPoint(arr):
maxX = - sys.maxsize;
maxY = - sys.maxsize;
n = len (arr);
for i in range (n):
maxX = max (maxX, arr[i][ 0 ]);
maxY = max (maxY, arr[i][ 1 ]);
for i in range (n):
if (maxX = = arr[i][ 0 ] and maxY = = arr[i][ 1 ]):
print ( "(" , maxX , ", " , maxY , ")" );
return ;
print ( - 1 );
if __name__ = = '__main__' :
arr = [[ 1 , 2 ], [ 2 , 1 ], [ 3 , 4 ], [ 4 , 3 ], [ 5 , 5 ]];
findMaxPoint(arr);
|
C#
using System;
class GFG{
static void findMaxPoint( int [,]arr)
{
int maxX = int .MinValue;
int maxY = int .MinValue;
int n = arr.GetLength(0);
for ( int i = 0; i < n; i++)
{
maxX = Math.Max(maxX, arr[i, 0]);
maxY = Math.Max(maxY, arr[i, 1]);
}
for ( int i = 0; i < n; i++)
{
if (maxX == arr[i, 0] &&
maxY == arr[i, 1])
{
Console.WriteLine( "(" + maxX + ", " +
maxY + ")" );
return ;
}
}
Console.WriteLine(-1);
}
public static void Main(String[] args)
{
int [,]arr = new int [,]{ { 1, 2 }, { 2, 1 },
{ 3, 4 }, { 4, 3 },
{ 5, 5 } };
findMaxPoint(arr);
}
}
|
Javascript
<script>
function findMaxPoint(arr){
let maxX = Number.MIN_VALUE;
let maxY = Number.MIN_VALUE;
let n = arr.length;
for (let i=0;i<n;i++){
maxX = Math.max(maxX, arr[i][0]);
maxY = Math.max(maxY, arr[i][1]);
}
for (let i=0;i<n;i++){
if (maxX == arr[i][0] && maxY == arr[i][1]){
document.write( "(" , maxX , ", " , maxY , ")" );
return ;
}
}
document.write(-1);
}
let arr = [[1, 2], [2, 1], [3, 4], [4, 3], [5, 5]];
findMaxPoint(arr);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...