using
System;
using
System.Collections.Generic;
class
GFG
{
static
void
findTriplets(
int
[] arr,
int
n)
{
Dictionary<
int
,
int
> mp =
new
Dictionary<
int
,
int
>();
for
(
int
x = 0; x < n; x++)
{
if
(mp.ContainsKey(arr[x]))
{
mp[arr[x]]++;
}
else
{
mp[arr[x]] = 1;
}
}
List<Tuple<
int
,
int
> > pq =
new
List<Tuple<
int
,
int
>>();
int
cnt = 0;
foreach
(KeyValuePair<
int
,
int
> pa
in
mp)
pq.Add(
new
Tuple<
int
,
int
>(pa.Value, pa.Key));
List<List<
int
>> ans =
new
List<List<
int
>>();
pq.Sort();
pq.Reverse();
while
(pq.Count >= 3)
{
Tuple<
int
,
int
>[] ar =
new
Tuple<
int
,
int
>[3];
for
(
int
x = 0; x < 3; x++)
{
ar[x] = pq[0];
pq.RemoveAt(0);
}
ans.Add(
new
List<
int
>());
ans[cnt].Add(ar[0].Item2);
ans[cnt].Add(ar[1].Item2);
ans[cnt].Add(ar[2].Item2);
for
(
int
x = 0; x < 3; x++)
{
ar[x] =
new
Tuple<
int
,
int
>(ar[x].Item1 - 1, ar[x].Item2);
if
(ar[x].Item1 != 0)
{
pq.Add(ar[x]);
pq.Sort();
pq.Reverse();
}
}
cnt++;
}
Console.Write(
"Maximum number of possible triples: "
);
Console.WriteLine(ans.Count);
foreach
(List<
int
> pa
in
ans)
{
foreach
(
int
v
in
pa)
Console.Write(v +
" "
);
Console.WriteLine();
}
}
static
void
Main()
{
int
[] arr = { 2, 2, 3, 3, 4, 4, 4, 4, 5 };
int
n = arr.Length;
findTriplets(arr, n);
}
}