# Count triplets such that sum of any two number is equal to third | Set 2

Given an array of *distinct positive integers* **arr[]** of length **N**, the task is to count all the triplets such that sum of two elements equals the third element.

**Examples:**

Input:arr[] = {1, 5, 3, 2}

Output:2

Explanation:

In the given array, there are two such triplets such that sum of the two numbers is equal to the third number, those are –

(1, 2, 3), (3, 2, 5)

Input:arr[] = {3, 2, 7}

Output:0

Explanation:

In the given array there are no such triplets such that sum of two numbers is equal to the third number.

**Approach:** The idea is to create a frequency array of the numbers which are present in the array and then check for each pair of the element that sum of the pair elements is present in the array or not with the help of frequency array in O(1) time.

**Algorithm:**

- Declare a
**frequency**array to store the frequency of the numbers. - Iterate over the elements of the array and increment the count of that number in the frequency array.
- Run two loops to choose two different indexes of the matrix and check that if the sum of the elements at those indexes is having frequency more than 0 in the frequency array.
If frequency of the sum is greater than 0: Increment the count of the triplets.

**Note**: We have assumed in the program that the value of array elements lies in the range [1, 100].

Below is the implementation of the above approach:

## C++

`// C++ implementation to count the ` `// triplets such that the sum of the ` `// two numbers is equal to third number ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the count of the ` `// triplets such that sum of two ` `// numbers is equal to the third number ` `int` `countTriplets(` `int` `arr[], ` `int` `n){ ` ` ` `int` `freq[100] = {0}; ` ` ` ` ` `// Loop to count the frequency ` ` ` `for` `(` `int` `i=0; i < n; i++){ ` ` ` `freq[arr[i]]++; ` ` ` `} ` ` ` `int` `count = 0; ` ` ` ` ` `// Loop to count for triplets ` ` ` `for` `(` `int` `i = 0;i < n; i++){ ` ` ` `for` `(` `int` `j = i+1; j < n; j++){ ` ` ` `if` `(freq[arr[i] + arr[j]]){ ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 4; ` ` ` `int` `arr[] = {1, 5, 3, 2}; ` ` ` ` ` `// Function Call ` ` ` `cout << countTriplets(arr, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to count the ` `// triplets such that the sum of the ` `// two numbers is equal to third number ` `class` `GFG{ ` ` ` `// Function to find the count of the ` `// triplets such that sum of two ` `// numbers is equal to the third number ` `static` `int` `countTriplets(` `int` `arr[], ` `int` `n){ ` ` ` `int` `[]freq = ` `new` `int` `[` `100` `]; ` ` ` ` ` `// Loop to count the frequency ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++){ ` ` ` `freq[arr[i]]++; ` ` ` `} ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `// Loop to count for triplets ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++){ ` ` ` `for` `(` `int` `j = i + ` `1` `; j < n; j++){ ` ` ` `if` `(freq[arr[i] + arr[j]] > ` `0` `){ ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `4` `; ` ` ` `int` `arr[] = {` `1` `, ` `5` `, ` `3` `, ` `2` `}; ` ` ` ` ` `// Function Call ` ` ` `System.out.print(countTriplets(arr, n)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 implementation to count the ` `# triplets such that the sum of the ` `# two numbers is equal to third number ` ` ` `# Function to find the count of the ` `# triplets such that sum of two ` `# numbers is equal to the third number ` `def` `countTriplets(arr, n): ` ` ` `freq ` `=` `[` `0` `for` `i ` `in` `range` `(` `100` `)] ` ` ` ` ` `# Loop to count the frequency ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `freq[arr[i]] ` `+` `=` `1` ` ` `count ` `=` `0` ` ` ` ` `# Loop to count for triplets ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `for` `j ` `in` `range` `(i ` `+` `1` `, n, ` `1` `): ` ` ` `if` `(freq[arr[i] ` `+` `arr[j]]): ` ` ` `count ` `+` `=` `1` ` ` `return` `count ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `4` ` ` `arr ` `=` `[` `1` `, ` `5` `, ` `3` `, ` `2` `] ` ` ` ` ` `# Function Call ` ` ` `print` `(countTriplets(arr, n)) ` ` ` `# This code is contributed by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to count the ` `// triplets such that the sum of the ` `// two numbers is equal to third number ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to find the count of the ` `// triplets such that sum of two ` `// numbers is equal to the third number ` `static` `int` `countTriplets(` `int` `[]arr, ` `int` `n){ ` ` ` `int` `[]freq = ` `new` `int` `[100]; ` ` ` ` ` `// Loop to count the frequency ` ` ` `for` `(` `int` `i = 0; i < n; i++){ ` ` ` `freq[arr[i]]++; ` ` ` `} ` ` ` `int` `count = 0; ` ` ` ` ` `// Loop to count for triplets ` ` ` `for` `(` `int` `i = 0; i < n; i++){ ` ` ` `for` `(` `int` `j = i + 1; j < n; j++){ ` ` ` `if` `(freq[arr[i] + arr[j]] > 0){ ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(` `string` `[] args) ` `{ ` ` ` `int` `n = 4; ` ` ` `int` `[]arr = {1, 5, 3, 2}; ` ` ` ` ` `// Function Call ` ` ` `Console.WriteLine(countTriplets(arr, n)); ` `} ` `} ` ` ` `// This code is contributed by Yahs_R ` |

*chevron_right*

*filter_none*

**Output:**

2

**Performance Analysis:**

**Time Complexity:**O(N^{2}).**Auxiliary Space:**O(N).

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 number of triplets with product equal to given number with duplicates allowed
- Count number of triplets with product equal to given number | Set 2
- Count number of triplets with product equal to given number
- Count triplets in a sorted doubly linked list whose product is equal to a given value x
- Number of triplets in array having subarray xor equal
- Count Substrings with equal number of 0s, 1s and 2s
- Count subarrays with equal number of 1's and 0's
- Count the numbers < N which have equal number of divisors as K
- Count subarrays with equal number of occurrences of two given elements
- Count the triplets such that A[i] < B[j] < C[k]
- Count of Triplets
- Count the number of strings in an array whose distinct characters are less than equal to M
- Count triplets in an array such that i<j<k and a[j] - a[i] = a[k] - a[j] = D
- Count of triplets in a given Array having GCD K
- Count of triplets in an Array (i, j, k) such that i < j < k and a[k] < a[i] < a[j]
- Count triplets with sum smaller than a given value
- Count the number of ways to divide an array into three contiguous parts having equal sum
- Count of triplets that satisfy the given equation
- Count Triplets such that one of the numbers can be written as sum of the other two
- Count of triplets from the given Array such that sum of any two elements is the third 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.