# Count of triplets from the given Array such that sum of any two elements is the third element

Given an unsorted array **arr**, the task is to find the count the distinct triplets in which the sum of any two elements is the third element.

**Examples:**

Input:arr[] = {1, 3, 4, 15, 19}

Output:2

Explanation:

In the given array there are two triplets such that sum of the any two element is equal to the third element: {{1, 3, 4}, {4, 15, 19}}

Input:arr[] = {7, 2, 5, 4, 3, 6, 1, 9, 10, 12}

Output:18

**Approach:**

- Sort the given array
- Create a Hash map for the array to check that a particular element is present or not.
- Iterate over the Array with two Loops to select any two elements at different positions and check that sum of those two elements is present in the hash map in O(1) time.
- If the Sum of the two element is present in the hash map then increment the count of the triplets.

Below is the implementation of the above approach:

## C++

`// C++ Implementation to find the ` `// Count the triplets in which sum ` `// of two elements is the third element ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// Function to find the count ` `// the triplets in which sum ` `// of two elements is the third element ` `int` `triplets(vector<` `int` `>arr) ` `{ ` ` ` ` ` `// Dictionary to check a element is ` ` ` `// present or not in array ` ` ` `map<` `int` `, ` `int` `> k; ` ` ` `map<pair<` `int` `, ` `int` `>, ` `int` `> mpp; ` ` ` ` ` `// List to check for ` ` ` `// duplicates of Triplets ` ` ` `vector<vector<` `int` `>> ssd; ` ` ` ` ` `// Set initial count to zero ` ` ` `int` `count = 0; ` ` ` ` ` `// Sort the array ` ` ` `sort(arr.begin(),arr.end()); ` ` ` ` ` `int` `i = 0; ` ` ` `while` `(i < arr.size()) ` ` ` `{ ` ` ` ` ` `// Add all the values as key ` ` ` `// value pairs to the dictionary ` ` ` `if` `(k.find(arr[i]) == k.end()) ` ` ` `k[arr[i]] = 1; ` ` ` ` ` `i += 1; ` ` ` `} ` ` ` ` ` `// Loop to choose two elements ` ` ` `int` `j = 0; ` ` ` `while` `(j < arr.size() - 1) ` ` ` `{ ` ` ` ` ` `int` `q = j + 1; ` ` ` `while` `(q < arr.size()) ` ` ` `{ ` ` ` ` ` `// Check for the sum and duplicate ` ` ` `if` `( k.find(arr[j] + arr[q]) != k.end() and ` ` ` `mpp[{arr[j], arr[q]}] != arr[j] + arr[q]) ` ` ` `{ ` ` ` ` ` `count += 1; ` ` ` ` ` `ssd.push_back({arr[j], arr[q], arr[j] + arr[q]}); ` ` ` `mpp[{arr[j], arr[q]}] = arr[j] + arr[q]; ` ` ` `} ` ` ` ` ` `q += 1; ` ` ` `} ` ` ` `j += 1; ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `vector<` `int` `>arr = {7, 2, 5, 4, 3, 6, 1, 9, 10, 12}; ` ` ` `int` `count = triplets(arr); ` ` ` `printf` `(` `"%d"` `,count); ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 Implementation to find the ` `# Count the triplets in which sum ` `# of two elements is the third element ` ` ` `# Function to find the count ` `# the triplets in which sum ` `# of two elements is the third element ` `def` `triplets(arr): ` ` ` ` ` `# Dictionary to check a element is ` ` ` `# present or not in array ` ` ` `k ` `=` `set` `() ` ` ` ` ` `# List to check for ` ` ` `# duplicates of Triplets ` ` ` `ssd ` `=` `[] ` ` ` ` ` `# Set initial count to zero ` ` ` `count ` `=` `0` ` ` ` ` `# Sort the array ` ` ` `arr.sort() ` ` ` ` ` `i ` `=` `0` ` ` `while` `i < ` `len` `(arr): ` ` ` ` ` `# Add all the values as key ` ` ` `# value pairs to the dictionary ` ` ` `if` `arr[i] ` `not` `in` `k: ` ` ` `k.add(arr[i]) ` ` ` ` ` `i ` `+` `=` `1` ` ` ` ` `# Loop to choose two elements ` ` ` `j ` `=` `0` ` ` `while` `j < ` `len` `(arr) ` `-` `1` `: ` ` ` ` ` `q ` `=` `j ` `+` `1` ` ` `while` `q < ` `len` `(arr): ` ` ` ` ` `# Check for the sum and duplicate ` ` ` `if` `arr[j] ` `+` `arr[q] ` `in` `k ` `and` `\ ` ` ` `[arr[j], arr[q], arr[j] ` `+` `arr[q]] ` `not` `in` `ssd: ` ` ` ` ` `count ` `+` `=` `1` ` ` ` ` `ssd.append([arr[j], arr[q], arr[j] ` `+` `arr[q]]) ` ` ` ` ` `q ` `+` `=` `1` ` ` `j ` `+` `=` `1` ` ` ` ` `return` `count ` ` ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `arr ` `=` `[` `7` `, ` `2` `, ` `5` `, ` `4` `, ` `3` `, ` `6` `, ` `1` `, ` `9` `, ` `10` `, ` `12` `] ` ` ` `count ` `=` `triplets(arr) ` ` ` `print` `(count) ` |

*chevron_right*

*filter_none*

**Output:**

18

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count elements in first Array with absolute difference greater than K with an element in second Array
- Count of elements in first Array greater than second Array with each element considered only once
- For each element in 1st array count elements less than or equal to it in 2nd array
- For each element in 1st array count elements less than or equal to it in 2nd array | Set 2
- Count of triplets of numbers 1 to N such that middle element is always largest
- Count elements that are divisible by at-least one element in another array
- Count of greater elements for each element in the Array
- Count the number of elements which are greater than any of element on right side of an array
- Count of operations to make all elements of array a[] equal to its min element by performing a[i] – b[i]
- Count of smaller elements on right side of each element in an Array using Merge sort
- Count of triplets in an Array (i, j, k) such that i < j < k and a[k] < a[i] < a[j]
- Count of triplets in a given Array having GCD K
- Count triplets in an array such that i<j<k and a[j] - a[i] = a[k] - a[j] = D
- Count of triplets in an array that satisfy the given conditions
- Form an array of distinct elements with each element as sum of an element from each array
- Count elements whose sum with K is greater than max element
- Count of triplets (a, b, c) in the Array such that a divides b and b divides c
- Find Kth element in an array containing odd elements first and then even elements
- Count of Array elements to be divided by 2 to make at least K elements equal
- Maximum count of elements divisible on the left for any element

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.