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(N^{3})

**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:

- Store the frequency of all the numbers in a Map.
- Make a max priority queue
*ans*to store pairs in it where the first element in pair is the**frequency**of some element and the second element in pair is the**element**itself. - Now repeatedly extract top 3 elements from the priority queue, make triplet using those 3 elements, decrease their frequency by 1 and again insert elements in priority queue using frequency is greater than 0.

Below is the implementation of the above approach:

## CPP

`// 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; ` `} ` |

*chevron_right*

*filter_none*

**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.

## Recommended Posts:

- Elements that occurred only once in the array
- Check if the string contains consecutive letters and each letter occurs exactly once
- Count the maximum number of elements that can be selected from the array
- Check if frequency of each element in given array is unique or not
- Modify the string such that it contains all vowels at least once
- Count the triplets such that A[i] < B[j] < C[k]
- Minimum deletions required to make frequency of each letter unique
- Count number of triplets with product equal to given number | Set 2
- Maximize the number of indices such that element is greater than element to its left
- Maximum number of unique values in the array after performing given operations
- Check if two items can be selected from two different categories without exceeding price
- Check if N numbers with Even Sum can be selected from a given Array
- Find if string is K-Palindrome or not using all characters exactly once
- Check if a cell can be visited more than once in a String
- Count number of triplets in an array having sum in the range [a, b]
- Count the number of unordered triplets with elements in increasing order and product less than or equal to integer X
- Minimum number of integers required such that each Segment contains at least one of them
- Count of pairs from arrays A and B such that element in A is greater than element in B at that index
- Count of distinct possible pairs such that the element from A is greater than the element from B
- Largest possible Subset from an Array such that no element is K times any other element in the Subset

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.