检查是否存在一对 (a, b),使得对于所有 N 对,任何一个元素都应该等于 a 或 b
给定一个由N对不同整数组成的数组arr[] ,任务是检查是否存在任何对(X, Y) ,使得数组的每一对都至少有一个与对(X, Y) 的公共元素。
例子:
Input: arr[] = {{1, 2}, {2, 3}, {3, 4}, {4, 5}}
Output: Yes
Explanation:
One of the pair that satisfies the condition is (2, 4).
Input: arr[] = {{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}}
Output: No
Explanation:
No pair satisfies the conditions.
方法:可以根据以下观察解决给定的问题:
- It can be observed that if there exists a pair (X, Y), then either arr[0].first will be equal to either X or Y, or arr[0].second will be equal to either X or Y.
- As each pair has distinct elements, then if an element occurs X times in X pairs, then it must exist in all the pairs.
- Therefore, if (P, Q) is the pair needed, and F is the count of pairs with no element common to P, then Q will be equal to an element which will occur F times in the pair where no element is common to P.
.请按照以下步骤解决问题:
- 初始化一个整数变量,比如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)