Given two arrays arr1[] and arr2[] of lengths N and M respectively, the task is to find the maximum number of pairs (i, j) such that 2 * arr1[i] ≤ arr2[j] (1 ≤ i ≤ N and 1 ≤ j ≤ M).
Note: Any array element can be part of a single pair.
Examples:
Input: N = 3, arr1[] = {3, 2, 1}, M = 4, arr2[] = {3, 4, 2, 1}
Output: 2
Explanation:
Only two pairs can be chosen:
- (1, 3): Choose elements arr1[3] and arr2[1].
Therefore, pair = (arr1[3], arr2[1]) = (1, 3). Also, 2 * arr1[3] ≤ arr2[1].
Now elements at positions 3 and 1 of arr1[] and arr2[] respectively, cannot be chosen.- (2, 2): Choose elements arr1[2] and arr2[2].
Therefore, pair = (arr1[2], arr2[2]) = (2, 4). Also, 2*arr1[2] <= arr2[2].
Now elements at position 2 of arr1[] and arr2[] cannot be chosen.Input: N = 1, arr1[] = {40}, M = 4, arr2[] = {10, 20, 30, 40}
Output: 0
Explanation:
No Possible Pair exists which satisfies the condition.
Naive Approach: The simplest approach is to first sort both the arrays and for each element arr1[i], greedily find the element that is just greater than or equal to the value 2 * arr1[i] in the given array arr2[] and then remove that element from arr2[] by incrementing the total number of required pairs by 1. After traversing the whole array arr1[], print the number of pairs.
Time Complexity: O(N * M), where N and M are the lengths of the given array.
Auxiliary Space: O(N+M)
Efficient Approach: The idea is to use the Greedy Algorithm by finding an element in arr2[] that is just greater than or equal to the value 2*arr1[i] where 0<=i<=N-1. Follow the below steps to solve the problem:
- Sort the array arr1[] and initialize a variable ans to store the maximum number of pairs.
- Add all the elements of arr2[] in Max Heap.
- Traverse the array arr1[] from i = (N – 1) to 0 in non-increasing order.
- For each element arr1[i], remove the peek element from the Max Heap until 2*arr1[i] becomes smaller than or equal to the peek element and increment ans by 1 if such element is found.
- After traversing the whole array, print ans as the maximum number of pairs.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Function to return the maximum// number of required pairsint numberOfPairs(int arr1[], int n, int arr2[], int m){ // Max Heap to add values of arr2[] priority_queue<int> pq; int i, j; // Sort the array arr1[] sort(arr1, arr1 + n); // Push all arr2[] into Max Heap for (j = 0; j < m; j++) { pq.push(arr2[j]); } // Initialize the ans int ans = 0; // Traverse the arr1[] in // decreasing order for (i = n - 1; i >= 0; i--) { // Remmove element until a // required pair is found if (pq.top() >= 2 * arr1[i]) { ans++; cout<<pq.top()<<"\n"; pq.pop(); } } // Return maximum number of pairs return ans;}// Driver Codeint main(){ // Given arrays int arr1[] = { 3, 1, 2 }; int arr2[] = { 2, 4, 3, 1 }; int N = sizeof(arr1) / sizeof(arr1[0]); int M = sizeof(arr2) / sizeof(arr2[0]); // Function Call cout << numberOfPairs(arr1, N, arr2, M); return 0;} |
Java
// Java program for the above approachimport java.io.*;import java.util.*;class GFG{ // Function to return the maximum// number of required pairsstatic int numberOfPairs(int[] arr1, int n, int[] arr2, int m){ // Max Heap to add values of arr2[] PriorityQueue<Integer> pQueue = new PriorityQueue<Integer>( new Comparator<Integer>() { public int compare(Integer lhs, Integer rhs) { if (lhs < rhs) { return + 1; } if (lhs.equals(rhs)) { return 0; } return -1; } }); int i, j; // Sort the array arr1[] Arrays.sort(arr1); // Push all arr2[] into Max Heap for(j = 0; j < m; j++) { pQueue.add(arr2[j]); } // Initialize the ans int ans = 0; // Traverse the arr1[] in // decreasing order for(i = n - 1; i >= 0; i--) { // Remove element until a // required pair is found if (pQueue.peek() >= 2 * arr1[i]) { ans++; pQueue.poll(); } } // Return maximum number of pairs return ans;}// Driver Code public static void main (String[] args){ // Given arrays int[] arr1 = { 3, 1, 2 }; int[] arr2 = { 2, 4, 3, 1 }; int N = 3; int M = 4; // Function call System.out.println(numberOfPairs(arr1, N, arr2, M));}}// This code is contributed by sallagondaavinashreddy7 |
2
Time Complexity: O(N*log N + M*log M), where N and M are the lengths of the given array.
Auxiliary Space: O(N+M)
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:
- Construct a Matrix with no element exceeding X and sum of two adjacent elements not exceeding Y
- Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K
- Maximize subsequences having array elements not exceeding length of the subsequence
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Count pairs from two arrays with difference exceeding K
- Split array into minimum number of subarrays having GCD of its first and last element exceeding 1
- Count of all possible numbers not exceeding M having suffix N
- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Minimize count of Subsets with difference between maximum and minimum element not exceeding K
- Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them
- Count pairs with bitwise XOR exceeding bitwise AND from a given array
- Priority queue of pairs in C++ with ordering by first and second element
- Find just strictly greater element from first array for each element in second array
- Count array elements with rank not exceeding K
- Count elements present in first array but not in second
- Count of elements in first Array greater than second Array with each element considered only once
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Count distinct elements after adding each element of First Array with Second Array
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K | Set 2
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.
Improved By : sallagondaavinashreddy7

