Find any two pairs (a, b) and (c, d) such that a d
Last Updated :
16 Sep, 2022
Given an array of pairs arr[] of size N, the task is to find any two pairs (a, b) and (c, d) such that a < c and b > d always holds. If any such pair exists, print those pairs. Otherwise, print “NO SUCH PAIR EXISTS“.
Examples:
Input: arr[] = {(3, 7), (21, 23), (4, 13), (1, 2), (7, -1)}
Output: Required pairs are (3, 7), (7, -1)
Explanation: (a, b) = (3, 7)
(c, d) = (7, -1)
Clearly, a < c and b > d.
Input: arr[]={(1, 6), (-5, 4), (10, 13)}
Output: NO SUCH PAIR EXIST
Naive Approach: The simplest approach to solve this problem is to check for every pair in the array if there exists any other pair which meets the given condition.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findPair(pair< int , int >* arr, int N)
{
for ( int i = 0; i < N; i++) {
int a = arr[i].first, b = arr[i].second;
for ( int j = i + 1; j < N; j++) {
int c = arr[j].first, d = arr[j].second;
if (a < c && b > d) {
cout << "(" << a << " " << b << "), ("
<< c << " " << d << ")\n" ;
return ;
}
}
}
cout << "NO SUCH PAIR EXIST\n" ;
}
int main()
{
pair< int , int > arr[] = {
{ 3, 7 }, { 21, 23 },
{ 4, 13 }, { 1, 2 },
{ 7, -1 }
};
findPair(arr, 5);
}
|
Java
import java.util.*;
class GFG
{
static class pair
{
int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static void findPair(pair arr[], int N)
{
for ( int i = 0 ; i < N; i++)
{
int a = arr[i].first, b = arr[i].second;
for ( int j = i + 1 ; j < N; j++)
{
int c = arr[j].first, d = arr[j].second;
if (a < c && b > d)
{
System.out.println( "(" + a + " " + b + "), ("
+ c + " " + d + ")" );
return ;
}
}
}
System.out.println( "NO SUCH PAIR EXIST" );
}
public static void main(String[] args)
{
pair arr[] = { new pair( 3 , 7 ), new pair( 21 , 23 ),
new pair( 4 , 13 ), new pair( 1 , 2 ),
new pair( 7 , - 1 )};
findPair(arr, 5 );
}
}
|
Python3
def findPair(arr, N):
for i in range (N):
a, b = arr[i][ 0 ], arr[i][ 1 ]
for j in range (i + 1 , N):
c, d = arr[j][ 0 ], arr[j][ 1 ]
if (a < c and b > d):
print ( "(" , a, b, "), (" , c, d, ")" )
return
print ( "NO SUCH PAIR EXIST" )
if __name__ = = '__main__' :
arr = [
[ 3 , 7 ], [ 21 , 23 ],
[ 4 , 13 ], [ 1 , 2 ],
[ 7 , - 1 ]
]
findPair(arr, 5 )
|
C#
using System;
public class GFG
{
class pair
{
public int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static void findPair(pair []arr, int N)
{
for ( int i = 0; i < N; i++)
{
int a = arr[i].first, b = arr[i].second;
for ( int j = i + 1; j < N; j++)
{
int c = arr[j].first, d = arr[j].second;
if (a < c && b > d)
{
Console.WriteLine( "(" + a + " " + b + "), ("
+ c + " " + d + ")" );
return ;
}
}
}
Console.WriteLine( "NO SUCH PAIR EXIST" );
}
public static void Main(String[] args)
{
pair []arr = { new pair( 3, 7 ), new pair( 21, 23 ),
new pair( 4, 13 ), new pair( 1, 2 ),
new pair( 7, -1 )};
findPair(arr, 5);
}
}
|
Javascript
<script>
class pair
{
constructor(first,second)
{
this .first=first;
this .second = second;
}
}
function findPair(arr,N)
{
for (let i = 0; i < N; i++)
{
let a = arr[i].first, b = arr[i].second;
for (let j = i + 1; j < N; j++)
{
let c = arr[j].first, d = arr[j].second;
if (a < c && b > d)
{
document.write( "(" + a + " " + b + "), ("
+ c + " " + d + ")<br>" );
return ;
}
}
}
document.write( "NO SUCH PAIR EXIST<br>" );
}
let arr=[ new pair( 3, 7 ), new pair( 21, 23 ),
new pair( 4, 13 ), new pair( 1, 2 ),
new pair( 7, -1 )];
findPair(arr, 5);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findPair(pair< int , int >* arr, int N)
{
sort(arr, arr + N);
for ( int i = 1; i < N; i++) {
int b = arr[i - 1].second;
int d = arr[i].second;
if (b > d) {
cout << "(" << arr[i - 1].first << " " << b
<< "), (" << arr[i].first << " " << d << ")" ;
return ;
}
}
cout << "NO SUCH PAIR EXIST\n" ;
}
int main()
{
pair< int , int > arr[] = {
{ 3, 7 }, { 21, 23 },
{ 4, 13 }, { 1, 2 },
{ 7, -1 }
};
findPair(arr, 5);
}
|
Java
import java.util.*;
class GFG
{
static class pair implements Comparable<pair>
{
int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
public int compareTo(pair p)
{
return this .first - p.first;
}
}
static void findpair(pair []arr, int N)
{
Arrays.sort(arr);
for ( int i = 1 ; i < N; i++)
{
int b = arr[i - 1 ].second;
int d = arr[i].second;
if (b > d)
{
System.out.print( "(" + arr[i - 1 ].first + " " + b
+ "), (" + arr[i].first + " " + d + ")" );
return ;
}
}
System.out.print( "NO SUCH PAIR EXIST\n" );
}
public static void main(String[] args)
{
pair arr[] = { new pair( 3 , 7 ), new pair( 21 , 23 ),
new pair( 4 , 13 ), new pair( 1 , 2 ),
new pair( 7 , - 1 ) };
findpair(arr, 5 );
}
}
|
Python3
def findPair(arr, N):
arr.sort(key = lambda x: x[ 0 ])
for i in range ( 1 , N):
b = arr[i - 1 ][ 1 ]
d = arr[i][ 1 ]
if (b > d):
print ( "(" , arr[i - 1 ][ 0 ], b, "), (" , arr[i][ 0 ], d, ")" )
return
print ( "NO SUCH PAIR EXIST\n" );
arr = [
[ 3 , 7 ], [ 21 , 23 ],
[ 4 , 13 ], [ 1 , 2 ],
[ 7 , - 1 ]]
findPair(arr, 5 )
|
C#
using System;
public class GFG
{
class pair : IComparable<pair>
{
public int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
public int CompareTo(pair p)
{
return this .first - p.first;
}
}
static void findpair(pair []arr, int N)
{
Array.Sort(arr);
for ( int i = 1; i < N; i++)
{
int b = arr[i - 1].second;
int d = arr[i].second;
if (b > d)
{
Console.Write( "(" + arr[i - 1].first + " " + b
+ "), (" + arr[i].first + " " + d + ")" );
return ;
}
}
Console.Write( "NO SUCH PAIR EXIST\n" );
}
public static void Main(String[] args)
{
pair []arr = { new pair( 3, 7 ), new pair( 21, 23 ),
new pair( 4, 13 ), new pair( 1, 2 ),
new pair( 7, -1 ) };
findpair(arr, 5);
}
}
|
Javascript
<script>
function findPair(arr, N)
{
arr.sort( function (a, b) {
return (a[0] - b[0]) || (a[1] - b[1]);
});
for (let i = 1; i < N; i++) {
let b = arr[i - 1][1];
let d = arr[i][1];
if (b > d) {
console.log( "(" + arr[i - 1][0] + " " + b
+ "), (" + arr[i][0] + " " +d + ")" );
return ;
}
}
console.log( "NO SUCH PAIR EXIST" );
}
var arr = [
[ 3, 7 ], [ 21, 23 ],
[ 4, 13 ], [ 1, 2 ],
[ 7, -1 ]
]
findPair(arr, 5);
</script>
|
Time Complexity: O(N*logN)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...