给定一个大小为N的对arr[]数组,任务是找到任意两对(a, b)和(c, d)使得a < c和b > d始终成立。如果存在任何这样的对,则打印这些对。否则,打印“ NO SUCH PAIR EXISTS ”。
例子:
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
朴素的方法:解决这个问题的最简单的方法是检查数组中的每一对是否存在满足给定条件的任何其他对。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find two pairs (a, b) and
// (c, d) such that a < c and b > d
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) {
cout << "(" << a << " " << b << "), ("
<< c << " " << d << ")\n";
return;
}
}
}
// If no such pair is found
cout << "NO SUCH PAIR EXIST\n";
}
// Driver Code
int main()
{
pair arr[] = {
{ 3, 7 }, { 21, 23 },
{ 4, 13 }, { 1, 2 },
{ 7, -1 }
};
findPair(arr, 5);
}
Java
// Java implementation to sort the
// array of points by its distance
// from the given point
import java.util.*;
class GFG
{
static class pair
{
int first, second;
public pair(int first, int second)
{
this.first = first;
this.second = second;
}
}
// Function to find two pairs (a, b) and
// (c, d) such that a < c and b > d
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;
}
}
}
// If no such pair is found
System.out.println("NO SUCH PAIR EXIST");
}
// Driver code
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);
}
}
// This code is contributed by sanjoy_62.
Python3
# Python3 program for the above approach
# Function to find two pairs (a, b) and
# (c, d) such that a < c and b > d
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
# If no such pair is found
print("NO SUCH PAIR EXIST")
# Driver Code
if __name__ == '__main__':
arr = [
[ 3, 7 ], [ 21, 23 ],
[ 4, 13 ], [ 1, 2 ],
[ 7, -1 ]
]
findPair(arr, 5)
# This code is contributed by mohit kumar 29
C#
// C# implementation to sort the
// array of points by its distance
// from the given point
using System;
public class GFG
{
class pair
{
public int first, second;
public pair(int first, int second)
{
this.first = first;
this.second = second;
}
}
// Function to find two pairs (a, b) and
// (c, d) such that a < c and b > d
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;
}
}
}
// If no such pair is found
Console.WriteLine("NO SUCH PAIR EXIST");
}
// Driver code
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);
}
}
// This code is contributed by 29AjayKumar
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find two pairs (a, b) and
// (c, d) such that a < c and b > d
void findPair(pair* arr, int N)
{
// Sort the array in increasing
// order of first element of pairs
sort(arr, arr + N);
// Traverse the array
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;
}
}
// If no such pair found
cout << "NO SUCH PAIR EXIST\n";
}
// Driver Code
int main()
{
pair arr[] = {
{ 3, 7 }, { 21, 23 },
{ 4, 13 }, { 1, 2 },
{ 7, -1 }
};
findPair(arr, 5);
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
static class pair implements Comparable
{
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;
}
}
// Function to find two pairs (a, b) and
// (c, d) such that a < c and b > d
static void findpair(pair []arr, int N)
{
// Sort the array in increasing
// order of first element of pairs
Arrays.sort(arr);
// Traverse the array
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;
}
}
// If no such pair found
System.out.print("NO SUCH PAIR EXIST\n");
}
// Driver Code
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);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach
# Function to find two pairs (a, b) and
# (c, d) such that a < c and b > d
def findPair(arr, N):
# Sort the array in increasing
# order of first element of pairs
arr.sort(key = lambda x: x[0])
# Traverse the array
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
#If no such pair found
print("NO SUCH PAIR EXIST\n");
# Driver Code
arr = [
[ 3, 7 ], [ 21, 23 ],
[ 4, 13 ], [ 1, 2 ],
[ 7, -1 ]]
findPair(arr, 5)
# This code is contributed by Dharanendra L V
C#
// C# program for the above approach
using System;
public class GFG
{
class pair : IComparable
{
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;
}
}
// Function to find two pairs (a, b) and
// (c, d) such that a < c and b > d
static void findpair(pair []arr, int N)
{
// Sort the array in increasing
// order of first element of pairs
Array.Sort(arr);
// Traverse the array
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;
}
}
// If no such pair found
Console.Write("NO SUCH PAIR EXIST\n");
}
// Driver Code
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);
}
}
// This code is contributed by 29AjayKumar
输出:
(3 7), (7 -1)
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法:优化上述方法,思路si解决问题:
- 根据每对的第一个元素对给定数组进行排序。
- 现在,任务简化为检查arr[]中是否存在任何对,其中arr[i].second < arr[i – 1].second作为每对的第一个元素已经排序。
- 因此,只需遍历数组并检查是否存在这样的一对。
- 如果发现是真的,打印这对。
- 否则,打印“NO SUCH PAIR EXIST”。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find two pairs (a, b) and
// (c, d) such that a < c and b > d
void findPair(pair* arr, int N)
{
// Sort the array in increasing
// order of first element of pairs
sort(arr, arr + N);
// Traverse the array
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;
}
}
// If no such pair found
cout << "NO SUCH PAIR EXIST\n";
}
// Driver Code
int main()
{
pair arr[] = {
{ 3, 7 }, { 21, 23 },
{ 4, 13 }, { 1, 2 },
{ 7, -1 }
};
findPair(arr, 5);
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
static class pair implements Comparable
{
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;
}
}
// Function to find two pairs (a, b) and
// (c, d) such that a < c and b > d
static void findpair(pair []arr, int N)
{
// Sort the array in increasing
// order of first element of pairs
Arrays.sort(arr);
// Traverse the array
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;
}
}
// If no such pair found
System.out.print("NO SUCH PAIR EXIST\n");
}
// Driver Code
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);
}
}
// This code is contributed by 29AjayKumar
蟒蛇3
# Python3 program for the above approach
# Function to find two pairs (a, b) and
# (c, d) such that a < c and b > d
def findPair(arr, N):
# Sort the array in increasing
# order of first element of pairs
arr.sort(key = lambda x: x[0])
# Traverse the array
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
#If no such pair found
print("NO SUCH PAIR EXIST\n");
# Driver Code
arr = [
[ 3, 7 ], [ 21, 23 ],
[ 4, 13 ], [ 1, 2 ],
[ 7, -1 ]]
findPair(arr, 5)
# This code is contributed by Dharanendra L V
C#
// C# program for the above approach
using System;
public class GFG
{
class pair : IComparable
{
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;
}
}
// Function to find two pairs (a, b) and
// (c, d) such that a < c and b > d
static void findpair(pair []arr, int N)
{
// Sort the array in increasing
// order of first element of pairs
Array.Sort(arr);
// Traverse the array
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;
}
}
// If no such pair found
Console.Write("NO SUCH PAIR EXIST\n");
}
// Driver Code
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);
}
}
// This code is contributed by 29AjayKumar
输出:
(4 13), (7 -1)
时间复杂度: O(N*logN)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live