Given a 2D array arr[][] consisting of N pairs of integers, the task is to find the pair which covers all other pairs of the given array. If it is impossible to find such a pair, then print -1.
A pair {a, ?b} will cover another pair {c, ?d}, if the condition (a???c???d???b) holds true.
Examples:
Input: arr[][2] = {{2, 2}, {3, 3}, {3, 5}, {4, 5}, {1, 1}, {1, 5}}
Output: 6
Explanation:
There exist a pair (1, 5) which cover all other pair because all other pair lies in the pair {1, 5}.
Therefore, the position of the pair {1, 5} is 6. So, the output is 6.
Input: arr[][] = {{1, 20}, {2, 22}, {3, 18}}
Output: -1
Explanation:
No such pair exists which covers all the remaining pairs.
Therefore, the output is -1
Naive Approach: The simplest approach is to compare each pair with all other pairs and check if any pair covers all the pairs or not. Below are the steps:
- Initialize a variable count = 0 which stores the number of pairs that lie between the current pair.
- Traverse the array of pairs and for each pair, check if the count is equal to the total number of pairs or not.
- If found to be true, it means that the pair can cover all other pairs. Print the pair.
- Otherwise, set count = 0 and repeat the above steps for the next pair.
- If no such pair exists, then print -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void position( int arr[][2], int N)
{
int pos = -1;
int count;
for ( int i = 0; i < N; i++) {
count = 0;
for ( int j = 0; j < N; j++) {
if (arr[i][0] <= arr[j][0]
&& arr[i][1] >= arr[j][1]) {
count++;
}
}
if (count == N) {
pos = i;
}
}
if (pos == -1) {
cout << pos;
}
else {
cout << pos + 1;
}
}
int main()
{
int arr[][2] = {{ 3, 3 }, { 1, 3 },
{ 2, 2 }, { 2, 3 },
{ 1, 2 }};
int N = sizeof (arr) / sizeof (arr[0]);
position(arr, N);
}
|
Java
import java.util.*;
class GFG{
static void position( int arr[][],
int N)
{
int pos = - 1 ;
int count;
for ( int i = 0 ; i < N; i++)
{
count = 0 ;
for ( int j = 0 ; j < N; j++)
{
if (arr[i][ 0 ] <= arr[j][ 0 ] &&
arr[i][ 1 ] >= arr[j][ 1 ])
{
count++;
}
}
if (count == N)
{
pos = i;
}
}
if (pos == - 1 )
{
System.out.print(pos);
}
else
{
System.out.print(pos + 1 );
}
}
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);
}
}
|
Python3
def position(arr, N):
pos = - 1 ;
count = 0 ;
for i in range (N):
count = 0 ;
for j in range (N):
if (arr[i][ 0 ] < = arr[j][ 0 ] and
arr[i][ 1 ] > = arr[j][ 1 ]):
count + = 1 ;
if (count = = N):
pos = i;
if (pos = = - 1 ):
print (pos);
else :
print (pos + 1 );
if __name__ = = '__main__' :
arr = [[ 3 , 3 ], [ 1 , 3 ],
[ 2 , 2 ], [ 2 , 3 ],
[ 1 , 2 ]];
N = len (arr);
position(arr, N);
|
C#
using System;
class GFG{
static void position( int [,] arr,
int N)
{
int pos = -1;
int count;
for ( int i = 0; i < N; i++)
{
count = 0;
for ( int j = 0; j < N; j++)
{
if (arr[i, 0] <= arr[j, 0] &&
arr[i, 1] >= arr[j, 1])
{
count++;
}
}
if (count == N)
{
pos = i;
}
}
if (pos == -1)
{
Console.Write(pos);
}
else
{
Console.Write(pos + 1);
}
}
public static void Main()
{
int [,] arr = {{3, 3}, {1, 3},
{2, 2}, {2, 3},
{1, 2}};
int N = arr.GetLength(0);
position(arr, N);
}
}
|
Javascript
<script>
function position(arr, N)
{
let pos = -1;
let count;
for (let i = 0; i < N; i++)
{
count = 0;
for (let j = 0; j < N; j++)
{
if (arr[i][0] <= arr[j][0] &&
arr[i][1] >= arr[j][1])
{
count++;
}
}
if (count == N)
{
pos = i;
}
}
if (pos == -1)
{
document.write(pos);
}
else
{
document.write(pos + 1);
}
}
let arr = [[3, 3], [1, 3],
[2, 2], [2, 3],
[1, 2]];
let N = arr.length;
position(arr, N);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to observe that the answer is always unique because there is always a unique pair that contains both minimum and maximum value. Below are the steps:
- Iterate over the given array of pairs and find the minimum first pair and maximum second pair from all the pairs in arr[][].
- After finding the maximum and minimum in the above step, there must exist any pair with arr[i][0] = minimum and arr[i][1] = maximum.
- Iterate through every array of pairs and check if there exists a pair whose arr[i][0] is equal to the minimum and arr[i][1] is equal to the maximum.
- If there exists any position in the above step, then print that position.
- Otherwise, print -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void position( int arr[][2], int N)
{
int pos = -1;
int right = INT_MIN;
int left = INT_MAX;
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;
}
}
cout << pos << endl;
}
int main()
{
int arr[][2] = {{ 3, 3 }, { 1, 3 },
{ 2, 2 }, { 2, 3 },
{ 1, 2 }};
int N = sizeof (arr) / sizeof (arr[0]);
position(arr, N);
}
|
Java
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 + "\n" );
}
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);
}
}
|
Python3
import sys
def position(arr, N):
pos = - 1
right = - sys.maxsize - 1
left = sys.maxsize
for i in range (N):
if (arr[i][ 1 ] > right):
right = arr[i][ 1 ]
if (arr[i][ 0 ] < left):
left = arr[i][ 0 ]
for i in range (N):
if (arr[i][ 0 ] = = left and
arr[i][ 1 ] = = right):
pos = i + 1
print (pos)
if __name__ = = '__main__' :
arr = [ [ 3 , 3 ], [ 1 , 3 ],
[ 2 , 2 ], [ 2 , 3 ],
[ 1 , 2 ] ]
N = len (arr)
position(arr, N)
|
C#
using System;
class GFG{
static void position( int [,]arr,
int N)
{
int pos = -1;
int right = int .MinValue;
int left = int .MaxValue;
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;
}
}
Console.Write(pos + "\n" );
}
public static void Main(String[] args)
{
int [,]arr = {{3, 3}, {1, 3},
{2, 2}, {2, 3},
{1, 2}};
int N = arr.GetLength(0);
position(arr, N);
}
}
|
Javascript
<script>
function position(arr, N){
let pos = -1
let right = Number.MIN_VALUE
let left = Number.MAX_VALUE
for (let i=0;i<N;i++){
if (arr[i][1] > right)
right = arr[i][1]
if (arr[i][0] < left)
left = arr[i][0]
}
for (let i=0;i<N;i++){
if (arr[i][0] == left && arr[i][1] == right)
pos = i + 1
}
document.write(pos, "</br>" )
}
let arr = [ [ 3, 3 ], [ 1, 3 ], [ 2, 2 ], [ 2, 3 ], [ 1, 2 ] ]
let N = arr.length
position(arr, N)
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)