📌  相关文章
📜  找出任意两对(a,b)和(c,d),使得a <c和b> d

📅  最后修改于: 2021-05-17 16:05:27             🧑  作者: Mango

给定大小为N的成对的arr []数组,任务是找到任意两对(a,b)(c,d) ,使得a b> d始终成立。如果存在任何这样的对,请打印这些对。否则,请打印“不存在任何配对”。

例子:

天真的方法:解决此问题的最简单方法是检查数组中的每个对是否存在满足给定条件的其他任何对。

下面是上述方法的实现:

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)

高效的方法:为了优化上述方法,解决问题的思路是:

  • 根据每对的第一个元素对给定的数组进行排序。
  • 现在,该任务简化为检查arr []中是否存在任何对,其中arr [i] .second 作为每对的第一个元素已被排序。
  • 因此,只需遍历数组并检查是否存在这样的对。
    • 如果发现是正确的,则打印该对。
    • 否则,打印“不存在任何配对”。

下面是上述方法的实现:

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

输出:
(4 13), (7 -1)

时间复杂度: O(N * logN)
辅助空间: O(1)