Check if there exists a pair (a, b) such that for all the N pairs either of the element should be equal to either a or b
Last Updated :
12 Jan, 2023
Given an array arr[] of N pairs of distinct integers, the task is to check if there exists any pair (X, Y) such that each pair of the array has at least one common element with the pair (X, Y).
Examples:
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.
Approach: The given problem can be solved based on the following observations:
- 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.
. Follow the steps below to solve the problem:
- Initialize an integer variable, say counter, to count the number of pairs where both the elements of the pair are not equal to X.
- Also, initialize a vector say frequency to store the frequency of the elements.
- Initialize a variable X as arr[0].first.
- Now traverse the array arr[], using the variable i, and perform the following steps:
- If arr[i].first and arr[i].second both are not equal to X, then the increment count of arr[i].first and arr[i].second in the vector frequency and then increment counter by 1.
- If the maximum frequency of the array frequency is equal to the counter, then print “Yes” and return.
- Otherwise, assign arr[0].second to X and then again perform the above steps.
- Finally, after completing the above steps, if none of the above cases satisfy, then print “No“.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string CommonPairs( int N, vector<pair< int , int > > arr)
{
vector< int > V = { arr[0].first, arr[0].second };
for ( int x : V) {
vector< int > frequency(N + 1, 0);
int counter = 0;
for ( auto c : arr) {
if (c.first != x && c.second != x) {
frequency++;
frequency++;
counter++;
}
}
int M = *max_element(frequency.begin(),
frequency.end());
if (M == counter) {
return "Yes" ;
}
}
return "No" ;
}
int main()
{
vector<pair< int , int > > arr
= { { 1, 2 }, { 2, 3 }, { 3, 4 }, { 4, 5 } };
int N = arr.size();
cout << CommonPairs(N, arr);
}
|
Java
import java.util.*;
class GFG {
static String CommonPairs( int N, int arr[][])
{
int V[] = { arr[ 0 ][ 0 ], arr[ 0 ][ 1 ] };
for ( int x : V) {
int [] frequency = new int [N + 2 ];
int counter = 0 ;
for ( int c[] : arr) {
if (c[ 0 ] != x && c[ 1 ] != x) {
frequency]++;
frequency]++;
counter++;
}
}
int M = Integer.MIN_VALUE;
for ( int i : frequency) {
M = Math.max(M, i);
}
if (M == counter) {
return "Yes" ;
}
}
return "No" ;
}
public static void main(String[] args)
{
int arr[][]
= { { 1 , 2 }, { 2 , 3 }, { 3 , 4 }, { 4 , 5 } };
int N = arr.length;
System.out.println(CommonPairs(N, arr));
}
}
|
Python3
def CommonPairs(N, arr):
V = (arr[ 0 ][ 0 ], arr[ 0 ][ 1 ])
for x in V:
frequency = [ 0 ] * (N + 2 )
counter = 0
for c in arr:
if (c[ 0 ] ! = x and c[ 1 ] ! = x):
frequency] + = 1
frequency] + = 1
counter + = 1
M = max (frequency)
if (M = = counter):
return "Yes"
return "No"
if __name__ = = "__main__" :
arr = [[ 1 , 2 ], [ 2 , 3 ], [ 3 , 4 ], [ 4 , 5 ]]
N = len (arr)
print (CommonPairs(N, arr))
|
C#
using System;
public class GFG
{
public static string CommonPairs( int N, int [,] arr)
{
int [] V = { arr[0, 0], arr[0, 1] };
foreach ( int x in V)
{
int [] frequency = new int [N + 2];
int counter = 0;
for ( int i = 0; i < arr.GetLength(0); i++)
{
if (arr[i, 0] != x && arr[i, 1] != x)
{
frequency[arr[i, 0]]++;
frequency[arr[i, 1]]++;
counter++;
}
}
int M = int .MinValue;
foreach ( int i in frequency)
{
M = Math.Max(M, i);
}
if (M == counter)
{
return "Yes" ;
}
}
return "No" ;
}
static public void Main ()
{
int [,] arr = { { 1, 2 }, { 2, 3 }, { 3, 4 }, { 4, 5 } };
int N = arr.GetLength(0);
Console.WriteLine(CommonPairs(N, arr));
}
}
|
Javascript
<script>
function CommonPairs(N, arr) {
let V = [arr[0][0], arr[0][1]];
for (let x of V) {
let frequency = new Array(N + 1).fill(0);
let counter = 0;
for (let c of arr) {
if (c[0] != x && c[1] != x) {
frequency]++;
frequency]++;
counter++;
}
}
let M = [...frequency].sort((a, b) => b - a)[0];
if (M == counter) {
return "Yes" ;
}
}
return "No" ;
}
let arr = [[1, 2], [2, 3], [3, 4], [4, 5]];
let N = arr.length;
document.write(CommonPairs(N, arr));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...