📌  相关文章
📜  检查是否存在一对 (a, b),使得对于所有 N 对,任何一个元素都应该等于 a 或 b

📅  最后修改于: 2022-05-13 01:57:48.987000             🧑  作者: Mango

检查是否存在一对 (a, b),使得对于所有 N 对,任何一个元素都应该等于 a 或 b

给定一个由N对不同整数组成的数组arr[] ,任务是检查是否存在任何对(X, Y) ,使得数组的每一对都至少有一个与对(X, Y) 的公共元素。

例子:

方法:可以根据以下观察解决给定的问题:

.请按照以下步骤解决问题:

  • 初始化一个整数变量,比如counter ,以计算对的两个元素都不等于X的对数。
  • 另外,初始化一个向量说频率来存储元素的频率。
  • 将变量X初始化为arr[0].first。
  • 现在使用变量i遍历数组arr[]并执行以下步骤:
    • 如果arr[ i].first 和arr[i].second都不等于X ,则arr [i].first 和arr[i].second在向量频率中的递增计数,然后将counter递增1
  • 如果数组频率的最大频率等于计数器,则打印“”并返回。
  • 否则,将arr[0].second分配给X ,然后再次执行上述步骤。
  • 最后,完成上述步骤后,如果以上情况都不满足,则打印“”。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check whether there exists
// a pair with given condition
string CommonPairs(int N, vector > arr)
{
    vector V = { arr[0].first, arr[0].second };
 
    for (int x : V) {
      
 
        // Stores the frequency of elements
        vector frequency(N + 1, 0);
 
        // Stores the count of the number
        // of pairs with no element equal
        // to x
        int counter = 0;
 
        // Traverse the array arr[]
        for (auto c : arr) {
            // If both the elements are not
            // equal to x
            if (c.first != x && c.second != x) {
 
                // Increment count of c.first
                // c.second by 1
                frequency++;
                frequency++;
 
                // Increment count by 1
                counter++;
            }
        }
 
        int M = *max_element(frequency.begin(),
                             frequency.end());
 
        // If maximum frequency is equal to counter
        if (M == counter) {
            return "Yes";
        }
    }
 
    // Return No
    return "No";
}
 
// Driver Code
int main()
{
    // Given Input
    vector > arr
        = { { 1, 2 }, { 2, 3 }, { 3, 4 }, { 4, 5 } };
    int N = arr.size();
 
    // Function Call
    cout << CommonPairs(N, arr);
}


Python3
# python 3 program for the above approach
 
# Function to check whether there exists
# a pair with given condition
def CommonPairs(N,  arr):
 
    V = (arr[0][0], arr[0][1])
 
    for x in V:
 
        # Stores the frequency of elements
        frequency = [0] * (N + 2)
 
        # Stores the count of the number
        # of pairs with no element equal
        # to x
        counter = 0
 
        # Traverse the array arr[]
        for c in arr:
           
            # If both the elements are not
            # equal to x
            if (c[0] != x and c[1] != x):
 
                # Increment count of c.first
                # c.second by 1
                frequency] += 1
                frequency] += 1
 
                # Increment count by 1
                counter += 1
 
        M = max(frequency)
 
        # If maximum frequency is equal to counter
        if (M == counter):
            return "Yes"
 
    # Return No
    return "No"
 
# Driver Code
if __name__ == "__main__":
   
    # Given Input
    arr = [[1, 2], [2, 3], [3, 4], [4, 5]]
    N = len(arr)
 
    # Function Call
    print(CommonPairs(N, arr))
 
    # This code is contributed by ukasp.


Javascript


输出
Yes

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