Given a vector of pairs arr[] and Q queries in the form of pairs in an array Queries[], the task for each query is to check if there exists any pair with both the values smaller than those in the pair of the current query. If found to be true, then print “Yes”. Otherwise, print “No”.
Examples:
Input: arr[][] = {{3, 5}, {2, 7}, {2, 3}, {4, 9}}, Queries[][] = {{3, 4}, {3, 2}, {4, 1}, {3, 7}}
Output:
Yes
No
No
Yes
Explanation:
Query 1: Pair {2, 3} exists in arr[] which has both the values smaller than {3, 4}.
Query 2: No valid pair found in arr[] for {3, 2}.
Query 3: No valid pair found in arr[] for {4, 1}.
Query 4: Pair {2, 7} exists in arr[] for {3, 7}.Input: arr[][] = {{2, 1}, {4, 2}, {4, 4}, {7, 2}}, Queries[][] = {{2, 1}, {1, 1}}
Output:
Yes
No
Naive Approach: The simplest approach is to traverse the array Queries[][] and for each pair, traverse the given array of pairs and check if there exists any such pair whose corresponding values is greater than or equal to the pair {p1, p2} then print “Yes”. Otherwise, print “No”.
Time Complexity: O(N*K)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to use Binary Search. Follow the steps below to solve this problem:
- Sort the array of pairs with respect to first element in the pairs in increasing order. If there exist 2 pairs whose first values are the same, then pairs are arranged on the basis of the second element of the pair.
- After sorting, traverse the array of pairs, and for all pairs having the same first value, replace the second value with the minimum of all the pairs having the same first value.
- Now, traverse the given Queries[] arrays and perform binary search on the array arr[] for each pair in it.
- If the pairs obtained from the above steps is smaller than the current pair in the Query[], then print “Yes” else print “No”.
Below is the implementation of the above approach:
C++14
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function that performs binary search // to find value less than or equal to // first value of the pair int binary_search(vector<pair<int, int> > vec, int n, int a) { int low, high, mid; low = 0; high = n - 1; // Perform binary search while (low < high) { // Find the mid mid = low + (high - low + 1) / 2; // Update the high if (vec[mid].first > a) { high = mid - 1; } // Else update low else if (vec[mid].first <= a) { low = mid; } } // Return the low index return low; } // Function to modify the second // value of each pair void modify_vec( vector<pair<int, int> >& v, int n) { // start from index 1 for (int i = 1; i < n; i++) { v[i].second = min(v[i].second, v[i - 1].second); } } // Function to evaluate each query int evaluate_query(vector<pair<int, int> > v, int n, int m1, int m2) { // Find value less than or equal to // the first value of pair int temp = binary_search(v, n, m1); // check if we got the required // pair or not if ((v[temp].first <= m1) && (v[temp].second <= m2)) { return 1; } return 0; } // Function to find a pair whose values is // less than the given pairs in query void checkPairs(vector<pair<int, int> >& v, vector<pair<int, int> >& queries) { // Find the size of the vector int n = v.size(); // sort the vector based on // the first value sort(v.begin(), v.end()); // Function Call to modify the // second value of each pair modify_vec(v, n); int k = queries.size(); // Traverse each queries for (int i = 0; i < k; i++) { int m1 = queries[i].first; int m2 = queries[i].second; // Evaluate each query int result = evaluate_query(v, n, m1, m2); // Print the result if (result > 0) cout << "Yes\n"; else cout << "No\n"; } } // Driver Code int main() { vector<pair<int, int> > arr = { { 3, 5 }, { 2, 7 }, { 2, 3 }, { 4, 9 } }; vector<pair<int, int> > queries = { { 3, 4 }, { 3, 2 }, { 4, 1 }, { 3, 7 } }; // Function Call checkPairs(arr, queries); return 0; } |
Yes No No Yes
Time Complexity: O(Q*log N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Queries to check if any non-repeating element exists within range [L, R] of an Array
- Pair with given product | Set 1 (Find if any pair exists)
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Queries for elements having values within the range A to B in the given index range using Segment Tree
- Split array into minimum number of subsets having maximum pair sum at most K
- Count of numbers upto N having absolute difference of at most K between any two adjacent digits
- Queries for elements having values within the range A to B using MO's Algorithm
- Check if a path exists in a tree with K vertices present or are at most at a distance D
- Queries to check if a path made up of even numbers from source to destination exists in a Matrix
- Most frequent element in Array after replacing given index by K for Q queries
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Check if a pair with given product exists in Linked list
- Check if a pair with given absolute difference exists in a Matrix
- Count of pairs having bit size at most X and Bitwise OR equal to X
- Maximize length of subarray having equal elements by adding at most K
- Check if product of every pair exists in an array
- Split an Array A[] into Subsets having equal Sum and sizes equal to elements of Array B[]
- Check if a path exists for a cell valued 1 to reach the bottom right corner of a Matrix before any cell valued 2
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Find minimum value of y for the given x values in Q queries from all the given set of lines
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

