Problem Explanation
You are given n
pairs of integers, and the task is to check if there exists any pair among these that contains both integers target_x
and target_y
in any order. If such a pair exists, the program should output “Yes”; otherwise, it should output “No”.
Approach
The approach to solving this problem is straightforward. First, we read the number of pairs n
and then store each pair in two separate vectors, a
and b
. These vectors represent the two elements of each pair. After reading all the pairs, we take the target integers target_x
and target_y
as input.
Next, we iterate through all the pairs and check if any pair matches the target integers in either of the two possible orders:
- If the first integer of the pair is equal to
target_x
and the second is equal totarget_y
. - If the first integer of the pair is equal to
target_y
and the second is equal totarget_x
.
If we find such a pair, we set a boolean flag found
to true
. After checking all the pairs, we simply output “Yes” if the flag is true
, otherwise “No”.
Complexity Analysis
-
Time Complexity: The time complexity of this solution is O(n), where
n
is the number of pairs. This is because we need to check each pair to see if it matches the target integers. -
Space Complexity: The space complexity is O(n) due to the storage of the pairs in the two vectors
a
andb
.
Edge Cases
- If
n
is0
, the program will immediately output “No” since there are no pairs to check. - The program correctly handles cases where
target_x
andtarget_y
are the same number, as it checks both possible orders within the pair.