📌  相关文章
📜  对,所有其他给定对在其最小值和最大值之间

📅  最后修改于: 2021-05-04 12:55:49             🧑  作者: Mango

给定一个由N对整数组成的2D数组arr [] [] ,任务是找到覆盖给定数组的所有其他对的那个对。如果找不到这样的一对,则打印-1

例子:

天真的方法:最简单的方法是将每对与所有其他对进行比较,并检查是否有任何对覆盖了所有对。步骤如下:

  1. 初始化一个变量count = 0 ,该变量存储位于当前对之间的数字对。
  2. 遍历对的数组,对于每个对,检查计数是否等于对的总数。
  3. 如果发现是真的,则意味着该对可以覆盖所有其他对。打印该对。
  4. 否则,将count设置为0 ,然后对下一对重复上述步骤。
  5. 如果不存在这样的对,则打印-1

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the position of
// the pair that covers every pair
// in the array arr[][]
void position(int arr[][2], int N)
{
    // Stores the index of the
    // resultant pair
    int pos = -1;
 
    // To count the occurences
    int count;
 
    // Iterate to check every pair
    for (int i = 0; i < N; i++) {
 
        // Set count to 0
        count = 0;
 
        for (int j = 0; j < N; j++) {
 
            // Condition to checked for
            // overlapping of pairs
            if (arr[i][0] <= arr[j][0]
                && arr[i][1] >= arr[j][1]) {
                count++;
            }
        }
 
        // If that pair can cover all other
        // pairs then store its position
        if (count == N) {
            pos = i;
        }
    }
 
    // If position not found
    if (pos == -1) {
 
        cout << pos;
    }
 
    // Otherwise
    else {
 
        cout << pos + 1;
    }
}
 
// Driver Code
int main()
{
    // Given array of pairs
    int arr[][2] = {{ 3, 3 }, { 1, 3 },
                    { 2, 2 }, { 2, 3 },
                    { 1, 2 }};
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    position(arr, N);
}


Java
// Java program for
// the above approach
import java.util.*;
class GFG{
 
// Function to find the position of
// the pair that covers every pair
// in the array arr[][]
static void position(int arr[][],
                     int N)
{
  // Stores the index of the
  // resultant pair
  int pos = -1;
 
  // To count the occurences
  int count;
 
  // Iterate to check every pair
  for (int i = 0; i < N; i++)
  {
    // Set count to 0
    count = 0;
 
    for (int j = 0; j < N; j++)
    {
      // Condition to checked for
      // overlapping of pairs
      if (arr[i][0] <= arr[j][0] &&
          arr[i][1] >= arr[j][1])
      {
        count++;
      }
    }
 
    // If that pair can cover all other
    // pairs then store its position
    if (count == N)
    {
      pos = i;
    }
  }
 
  // If position not found
  if (pos == -1)
  {
    System.out.print(pos);
  }
 
  // Otherwise
  else
  {
    System.out.print(pos + 1);
  }
}
 
// Driver Code
public static void main(String[] args)
{
  // Given array of pairs
  int arr[][] = {{3, 3}, {1, 3},
                 {2, 2}, {2, 3},
                 {1, 2}};
 
  int N = arr.length;
 
  // Function Call
  position(arr, N);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for
# the above approach
 
# Function to find the position of
# the pair that covers every pair
# in the array arr
def position(arr, N):
   
    # Stores the index of the
    # resultant pair
    pos = -1;
 
    # To count the occurences
    count = 0;
 
    # Iterate to check every pair
    for i in range(N):
       
        # Set count to 0
        count = 0;
 
        for j in range(N):
            # Condition to checked for
            # overlapping of pairs
            if (arr[i][0] <= arr[j][0] and
                arr[i][1] >= arr[j][1]):
                count += 1;
 
        # If that pair can cover
        # all other pairs then
        # store its position
        if (count == N):
            pos = i;
 
    # If position not found
    if (pos == -1):
        print(pos);
 
    # Otherwise
    else:
        print(pos + 1);
 
# Driver Code
if __name__ == '__main__':
   
    # Given array of pairs
    arr = [[3, 3], [1, 3],
           [2, 2], [2, 3],
           [1, 2]];
 
    N = len(arr);
 
    # Function Call
    position(arr, N);
 
# This code is contributed by shikhasingrajput


C#
// C# program for
// the above approach
using System;
class GFG{
  
// Function to find the position of
// the pair that covers every pair
// in the array arr[][]
static void position(int[,] arr,
                     int N)
{
  // Stores the index of the
  // resultant pair
  int pos = -1;
 
  // To count the occurences
  int count;
 
  // Iterate to check every pair
  for(int i = 0; i < N; i++)
  {
    // Set count to 0
    count = 0;
 
    for(int j = 0; j < N; j++)
    {
      // Condition to checked for
      // overlapping of pairs
      if (arr[i, 0] <= arr[j, 0] &&
          arr[i, 1] >= arr[j, 1])
      {
        count++;
      }
    }
 
    // If that pair can cover
    // all other pairs then
    // store its position
    if (count == N)
    {
      pos = i;
    }
  }
 
  // If position not found
  if (pos == -1)
  {
    Console.Write(pos);
  }
 
  // Otherwise
  else
  {
    Console.Write(pos + 1);
  }
}
 
// Driver Code
public static void Main()
{ 
  // Give array of pairs
  int[,] arr = {{3, 3}, {1, 3},
                {2, 2}, {2, 3},
                {1, 2}};
 
  int N = arr.GetLength(0);
 
  // Function Call
  position(arr, N);
}
}
 
// This code is contributed by sanjoy_62


C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the position of
// the pair that covers every pair
// in the array arr[][]
void position(int arr[][2], int N)
{
    // Postion to store the index
    int pos = -1;
 
    // Stores the maximum second value
    int right = INT_MIN;
 
    // Stores the minimum first value
    int left = INT_MAX;
 
    // Iterate over the array of pairs
    for (int i = 0; i < N; i++) {
 
        // Update right maximum
        if (arr[i][1] > right) {
            right = arr[i][1];
        }
 
        // Update left minimum
        if (arr[i][0] < left) {
 
            left = arr[i][0];
        }
    }
 
    // Iterate over the array of pairs
    for (int i = 0; i < N; i++) {
 
        // If any pair exists with value
        // {left, right} then store it
        if (arr[i][0] == left
            && arr[i][1] == right) {
 
            pos = i + 1;
        }
    }
 
    // Print the answer
    cout << pos << endl;
}
 
// Driver Code
int main()
{
    // Given array of pairs
    int arr[][2] = {{ 3, 3 }, { 1, 3 },
                    { 2, 2 }, { 2, 3 },
                    { 1, 2 }};
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    position(arr, N);
}


Java
// Java program for the
// above approach
import java.util.*;
class GFG{
 
// Function to find the position
// of the pair that covers
// every pair in the array arr[][]
static void position(int arr[][],
                     int N)
{
  // Postion to store
  // the index
  int pos = -1;
 
  // Stores the maximum
  // second value
  int right = Integer.MIN_VALUE;
 
  // Stores the minimum
  // first value
  int left = Integer.MAX_VALUE;
 
  // Iterate over the array
  // of pairs
  for (int i = 0; i < N; i++)
  {
    // Update right maximum
    if (arr[i][1] > right)
    {
      right = arr[i][1];
    }
 
    // Update left minimum
    if (arr[i][0] < left)
    {
      left = arr[i][0];
    }
  }
 
  // Iterate over the array
  // of pairs
  for (int i = 0; i < N; i++)
  {
    // If any pair exists
    // with value {left,
    // right} then store it
    if (arr[i][0] == left &&
        arr[i][1] == right)
    {
      pos = i + 1;
    }
  }
 
  // Print the answer
  System.out.print(pos + "\n");
}
 
// Driver Code
public static void main(String[] args)
{
  // Given array of pairs
  int arr[][] = {{3, 3}, {1, 3},
                 {2, 2}, {2, 3},
                 {1, 2}};
 
  int N = arr.length;
 
  // Function Call
  position(arr, N);
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 program for the above approach
import sys
 
# Function to find the position of
# the pair that covers every pair
# in the array arr[][]
def position(arr, N):
     
    # Postion to store the index
    pos = -1
 
    # Stores the minimum second value
    right = -sys.maxsize - 1
 
    # Stores the maximum first value
    left = sys.maxsize
 
    # Iterate over the array of pairs
    for i in range(N):
 
        # Update right maximum
        if (arr[i][1] > right):
            right = arr[i][1]
 
        # Update left minimum
        if (arr[i][0] < left):
            left = arr[i][0]
 
    # Iterate over the array of pairs
    for i in range(N):
 
        # If any pair exists with value
        # {left, right then store it
        if (arr[i][0] == left and
            arr[i][1] == right):
            pos = i + 1
 
    # Print the answer
    print(pos)
 
# Driver Code
if __name__ == '__main__':
     
    # Given array of pairs
    arr = [ [ 3, 3 ], [ 1, 3 ],
            [ 2, 2 ], [ 2, 3 ],
            [ 1, 2 ] ]
 
    N = len(arr)
 
    # Function call
    position(arr, N)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the
// above approach
using System;
class GFG{
 
// Function to find the position
// of the pair that covers
// every pair in the array [,]arr
static void position(int [,]arr,
                     int N)
{
  // Postion to store
  // the index
  int pos = -1;
 
  // Stores the maximum
  // second value
  int right = int.MinValue;
 
  // Stores the minimum
  // first value
  int left = int.MaxValue;
 
  // Iterate over the array
  // of pairs
  for (int i = 0; i < N; i++)
  {
    // Update right maximum
    if (arr[i, 1] > right)
    {
      right = arr[i, 1];
    }
 
    // Update left minimum
    if (arr[i, 0] < left)
    {
      left = arr[i, 0];
    }
  }
 
  // Iterate over the array
  // of pairs
  for (int i = 0; i < N; i++)
  {
    // If any pair exists
    // with value {left,
    // right} then store it
    if (arr[i, 0] == left &&
        arr[i, 1] == right)
    {
      pos = i + 1;
    }
  }
 
  // Print the answer
  Console.Write(pos + "\n");
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given array of pairs
  int [,]arr = {{3, 3}, {1, 3},
                {2, 2}, {2, 3},
                {1, 2}};
 
  int N = arr.GetLength(0);
 
  // Function Call
  position(arr, N);
}
}
 
// This code is contributed by shikhasingrajput


输出
2


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

高效的方法:为了优化上述方法,我们的想法是观察答案始终是唯一的,因为总会有一个包含最小值和最大值的唯一对。步骤如下:

  1. 遍历给定的对对数组,并从arr [] []中的所有对中找到最小的第一对和最大的第二对。
  2. 在上述步骤中找到最大值和最小值之后,必须存在任何对,其中arr [i] [0] =最小值和arr [i] [1] =最大值。
  3. 遍历每个对对,并检查是否存在一对arr [i] [0]等于最小值,而arr [i] [1]等于最大值。
  4. 如果上述步骤中存在任何位置,请打印该位置。
  5. 否则,打印-1

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the position of
// the pair that covers every pair
// in the array arr[][]
void position(int arr[][2], int N)
{
    // Postion to store the index
    int pos = -1;
 
    // Stores the maximum second value
    int right = INT_MIN;
 
    // Stores the minimum first value
    int left = INT_MAX;
 
    // Iterate over the array of pairs
    for (int i = 0; i < N; i++) {
 
        // Update right maximum
        if (arr[i][1] > right) {
            right = arr[i][1];
        }
 
        // Update left minimum
        if (arr[i][0] < left) {
 
            left = arr[i][0];
        }
    }
 
    // Iterate over the array of pairs
    for (int i = 0; i < N; i++) {
 
        // If any pair exists with value
        // {left, right} then store it
        if (arr[i][0] == left
            && arr[i][1] == right) {
 
            pos = i + 1;
        }
    }
 
    // Print the answer
    cout << pos << endl;
}
 
// Driver Code
int main()
{
    // Given array of pairs
    int arr[][2] = {{ 3, 3 }, { 1, 3 },
                    { 2, 2 }, { 2, 3 },
                    { 1, 2 }};
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    position(arr, N);
}

Java

// Java program for the
// above approach
import java.util.*;
class GFG{
 
// Function to find the position
// of the pair that covers
// every pair in the array arr[][]
static void position(int arr[][],
                     int N)
{
  // Postion to store
  // the index
  int pos = -1;
 
  // Stores the maximum
  // second value
  int right = Integer.MIN_VALUE;
 
  // Stores the minimum
  // first value
  int left = Integer.MAX_VALUE;
 
  // Iterate over the array
  // of pairs
  for (int i = 0; i < N; i++)
  {
    // Update right maximum
    if (arr[i][1] > right)
    {
      right = arr[i][1];
    }
 
    // Update left minimum
    if (arr[i][0] < left)
    {
      left = arr[i][0];
    }
  }
 
  // Iterate over the array
  // of pairs
  for (int i = 0; i < N; i++)
  {
    // If any pair exists
    // with value {left,
    // right} then store it
    if (arr[i][0] == left &&
        arr[i][1] == right)
    {
      pos = i + 1;
    }
  }
 
  // Print the answer
  System.out.print(pos + "\n");
}
 
// Driver Code
public static void main(String[] args)
{
  // Given array of pairs
  int arr[][] = {{3, 3}, {1, 3},
                 {2, 2}, {2, 3},
                 {1, 2}};
 
  int N = arr.length;
 
  // Function Call
  position(arr, N);
}
}
 
// This code is contributed by Princi Singh

Python3

# Python3 program for the above approach
import sys
 
# Function to find the position of
# the pair that covers every pair
# in the array arr[][]
def position(arr, N):
     
    # Postion to store the index
    pos = -1
 
    # Stores the minimum second value
    right = -sys.maxsize - 1
 
    # Stores the maximum first value
    left = sys.maxsize
 
    # Iterate over the array of pairs
    for i in range(N):
 
        # Update right maximum
        if (arr[i][1] > right):
            right = arr[i][1]
 
        # Update left minimum
        if (arr[i][0] < left):
            left = arr[i][0]
 
    # Iterate over the array of pairs
    for i in range(N):
 
        # If any pair exists with value
        # {left, right then store it
        if (arr[i][0] == left and
            arr[i][1] == right):
            pos = i + 1
 
    # Print the answer
    print(pos)
 
# Driver Code
if __name__ == '__main__':
     
    # Given array of pairs
    arr = [ [ 3, 3 ], [ 1, 3 ],
            [ 2, 2 ], [ 2, 3 ],
            [ 1, 2 ] ]
 
    N = len(arr)
 
    # Function call
    position(arr, N)
 
# This code is contributed by mohit kumar 29

C#

// C# program for the
// above approach
using System;
class GFG{
 
// Function to find the position
// of the pair that covers
// every pair in the array [,]arr
static void position(int [,]arr,
                     int N)
{
  // Postion to store
  // the index
  int pos = -1;
 
  // Stores the maximum
  // second value
  int right = int.MinValue;
 
  // Stores the minimum
  // first value
  int left = int.MaxValue;
 
  // Iterate over the array
  // of pairs
  for (int i = 0; i < N; i++)
  {
    // Update right maximum
    if (arr[i, 1] > right)
    {
      right = arr[i, 1];
    }
 
    // Update left minimum
    if (arr[i, 0] < left)
    {
      left = arr[i, 0];
    }
  }
 
  // Iterate over the array
  // of pairs
  for (int i = 0; i < N; i++)
  {
    // If any pair exists
    // with value {left,
    // right} then store it
    if (arr[i, 0] == left &&
        arr[i, 1] == right)
    {
      pos = i + 1;
    }
  }
 
  // Print the answer
  Console.Write(pos + "\n");
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given array of pairs
  int [,]arr = {{3, 3}, {1, 3},
                {2, 2}, {2, 3},
                {1, 2}};
 
  int N = arr.GetLength(0);
 
  // Function Call
  position(arr, N);
}
}
 
// This code is contributed by shikhasingrajput
输出
2



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