
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count maximum number of disjoint pairs having one element not less than k times the other in C++
In this article, we will discuss how to count the maximum number of disjoint pairs in an array, where for each pair (x, y), one element is not less than K times the other. The goal is to find the number of such valid pairs in the array using C++. Given an array of integers, the task is to find all pairs (i, j) such that:
- arr[i] > K * arr[j] or arr[j] > K * arr[i] holds true for a given constant K.
- The condition is that no element should be reused in more than one pair(the pairs must be disjoint).
Let's understand with an example -
For an array arr = [10, 3, 5, 20, 8] and K = 2, the output will be 2 disjoint pairs. Here's how: Pair (10, 5): 10 <= 2 * 5 ? Valid pair Pair (20, 3): 20 <= 2 * 3 ? Valid pair No other valid pairs can be formed without reusing elements, which makes the pairs disjoint.
Approaches to Solve This Problem
In this section, we will look at different ways to solve this problem. We will cover two main methods:
Using Sorting and Two Pointers
In this approach, we first sort the array in ascending order. Then, we use two pointers to find valid pairs. We check each element to see if it forms a valid pair with another, and if it does, we count the pair and move the pointers.
Example
In this example, we sort the array and use two pointers. One pointer starts at the smallest number, and the other starts at the largest. We check if the larger number is at least K times the smaller number. If it is, we count the pair and move both pointers. If not, we just move the left pointer forward.
#include <iostream> #include <vector> #include <algorithm> using namespace std; int countPairs(vector<int>& arr, int K) { sort(arr.begin(), arr.end()); // Sort the array int left = 0, right = arr.size() - 1, count = 0; while (left < right) { // Check if arr[right] is at least K times arr[left] if (arr[right] >= K * arr[left]) { count++; // Pair found, move both pointers left++; right--; } else { left++; // Move the left pointer forward } } return count; } int main() { vector<int> arr = {10, 3, 5, 20, 8}; int K = 2; cout << "Maximum number of disjoint pairs: " << countPairs(arr, K) << endl; return 0; }
The below output shows the maximum number of disjoint pairs that can be formed.
Maximum number of disjoint pairs: 2
Using a Hash Map
In this approach, we use a hash map to store the frequency of each element. For each element, we check if there is another element such that one element is not less than K times the other. If such a pair is found, we decrease the frequency of both elements to prevent reuse.
Example
In this example, we loop through the array and track the frequency of each element in a hash map. For each element, we check if there's another element that is not less than K times the first element (or vice versa). If a valid pair is found, we mark both elements as used and move to the next pair.
#include <iostream> #include <vector> #include <unordered_map> using namespace std; int countPairs(vector<int>& arr, int K) { unordered_map<int, int> freq; int count = 0; // Count the frequency of each element for (int num : arr) { freq[num]++; } // Try to form pairs for (auto& entry : freq) { int num = entry.first; if (freq[num] > 0) { // Check if there exists a valid pair for (auto& other : freq) { if (other.first != num && other.second > 0) { if (num >= K * other.first || other.first >= K * num) { count++; // Valid pair found freq[num]--; freq[other.first]--; break; // Move to next element after forming the pair } } } } } return count; } int main() { vector<int> arr = {1, 9, 4, 7, 3}; int K = 2; cout << "Maximum number of disjoint pairs: " << countPairs(arr, K) << endl; return 0; }
The output of this approach is shown below, which displays the maximum number of disjoint pairs:
Maximum number of disjoint pairs: 2
Conclusion
In this article, we learned how to count the maximum number of disjoint pairs in an array where one element is at least k times the other. We discussed two approaches: using sorting with two pointers and using a hash map to track frequencies. Both methods help in solving the problem efficiently.