Given an array arr[] of size N. Find the maximum number of triplets that can be made using array elements such that all elements in each triplet are different. Print the maximum number of possible triplets along with a list of the triplets.
Note: Each element of the array can belong to only 1 triplet.
Examples:
Input:_ arr[] = {2, 2, 3, 3, 4, 4, 4, 4, 5}_
Output:
Maximum number of possible triples : 2
2 3 4
3 4 5
Explanation:
We can form atmost 2 triples using the given array such that each triple contains different elements.
Input:_ arr[] = {1, 2, 3, 4, 5, 6, 7 }_
Output:
Maximum number of possible triples : 2
5 6 7
2 3 4
Explanation:
We can form atmost 2 triples using the given array such that each triple contains different elements.
Naive Approach: The idea is to run three nested loops to generate all triplets and for every triplet, check if they are pairwise distinct and also check if each element of the array belongs to exactly 1 triplet.
Time Complexity:_ O(N3)_
Auxiliary Space:_ O(1)_
Efficient Approach: The problem can be solved Greedy Approach and keep taking triplets having a maximum frequency. Below are the steps:
Below is the implementation of the above approach:
// C++ program for the above approach
#include <bits/stdc++.h>
**using**
**namespace**
std;
// Function that finds maximum number
// of triplets with different elements
**void**
findTriplets(``**int**
ar[],
**int**
n)
{
// Map M will store the frequency
// of each element in the array
unordered_map<``**int**``,
**int**``> mp;
**for**
(``**int**
x = 0; x < n; x++)
mp[ar[x]]++;
// Priority queue of pairs
// {frequency, value}
priority_queue<pair<``**int**``,
**int**``> > pq;
**for**
(``**auto**``& pa : mp)
pq.push({ pa.second, pa.first });
// ans will store possible triplets
vector<array<``**int**``, 3> > ans;
**while**
(pq.size() >= 3) {
// Extract top 3 elements
pair<``**int**``,
**int**``> ar[3];
**for**
(``**int**
x = 0; x < 3; x++) {
ar[x] = pq.top();
pq.pop();
}
// Make a triplet
ans.push_back({ ar[0].second,
ar[1].second,
ar[2].second });
// Decrease frequency and push
// back into priority queue if
// non-zero frequency
**for**
(``**int**
x = 0; x < 3; x++) {
ar[x].first--;
**if**
(ar[x].first)
pq.push(ar[x]);
}
}
// Print the triplets
cout <<
"Maximum number of "
<<
"possible triples: "``;
cout << ans.size() << endl;
**for**
(``**auto**``& pa : ans) {
// Print the triplets
**for**
(``**int**
v : pa)
cout << v <<
" "``;
cout << endl;
}
}
// Driver Code
**int**
main()
{
// Given array arr[]
**int**
arr[] = { 2, 2, 3, 3, 4, 4, 4, 4, 5 };
**int**
n =
**sizeof**``(arr) /
**sizeof**``(arr[0]);
// Function Call
findTriplets(arr, n);
**return**
0;
}
Output:
Maximum number of possible triples: 2
4 3 2
4 5 3
Time Complexity:_ O(N*log N)_
Auxiliary Space:_ O(N)_
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.
#arrays #greedy #sorting #cpp-priority-queue #priority-queue