// C# Implementation to find the
// Count the triplets in which sum
// of two elements is the third element
using System;
using System.Collections.Generic;
class GFG {
// Function to find the count
// the triplets in which sum
// of two elements is the third element
static int triplets(List<int> arr)
{
// Dictionary to check a element is
// present or not in array
Dictionary<int, int> k = new Dictionary<int, int>();
Dictionary<Tuple<int, int>, int> mpp = new Dictionary<Tuple<int, int>, int>();
// List to check for
// duplicates of Triplets
List<List<int>> ssd = new List<List<int>>();
// Set initial count to zero
int count = 0;
// Sort the array
arr.Sort();
int i = 0;
while (i < arr.Count)
{
// Add all the values as key
// value pairs to the dictionary
if (!k.ContainsKey(arr[i]))
k[arr[i]] = 1;
i += 1;
}
// Loop to choose two elements
int j = 0;
while (j < arr.Count - 1)
{
int q = j + 1;
while (q < arr.Count)
{
// Check for the sum and duplicate
if(!k.ContainsKey(arr[j] + arr[q]))
{
q += 1;
continue;
}
if (mpp.ContainsKey(new Tuple<int,int>(arr[j], arr[q])) &&
mpp[new Tuple<int,int>(arr[j], arr[q])] != (arr[j] + arr[q]))
{
count += 1;
ssd.Add(new List<int>(new int[]{arr[j], arr[q], arr[j] + arr[q]}));
mpp[new Tuple<int,int>(arr[j], arr[q])] = arr[j] + arr[q];
}
else{
count += 1;
ssd.Add(new List<int>(new int[]{arr[j], arr[q], arr[j] + arr[q]}));
mpp[new Tuple<int,int>(arr[j], arr[q])] = arr[j] + arr[q];
}
q += 1;
}
j += 1;
}
return count;
}
// Driver code
static void Main()
{
List<int> arr = new List<int>(new int[]{7, 2, 5, 4, 3, 6, 1, 9, 10, 12});
int count = triplets(arr);
Console.Write(count);
}
}
// This code is contributed by divyeshrabadiya07