Count triplets such that sum of any two number is equal to third | Set 2
Last Updated :
20 Jan, 2023
Given an array of distinct positive integers arr[] of length N, the task is to count all the triplets such that the 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.
Naive approach: Generate all the triplets of the given array and check the sum of two elements to equal the third element.
C++
#include <bits/stdc++.h>
using namespace std;
int countTriplets( int arr[], int n)
{
int count = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
for ( int k = j + 1; k < n; k++) {
if (arr[i] + arr[j] == arr[k]) {
count++;
}
else if (arr[i] + arr[k] == arr[j]) {
count++;
}
else if (arr[j] + arr[k] == arr[i]) {
count++;
}
}
}
}
return count;
}
int main()
{
int n = 4;
int arr[] = { 1, 5, 3, 2 };
cout << countTriplets(arr, n);
return 0;
}
|
Java
public class Gfg {
public static int countTriplets( int [] arr, int n)
{
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = i + 1 ; j < n; j++) {
for ( int k = j + 1 ; k < n; k++) {
if (arr[i] + arr[j] == arr[k]) {
count++;
}
else if (arr[i] + arr[k] == arr[j]) {
count++;
}
else if (arr[j] + arr[k] == arr[i]) {
count++;
}
}
}
}
return count;
}
public static void main(String[] args)
{
int n = 4 ;
int [] arr = { 1 , 5 , 3 , 2 };
System.out.println(countTriplets(arr, n));
}
}
|
Python3
def countTriplets(arr, n):
count = 0 ;
for i in range ( 0 ,n):
for j in range (i + 1 , n):
for k in range (j + 1 ,n):
if (arr[i] + arr[j] = = arr[k]):
count + = 1 ;
elif (arr[i] + arr[k] = = arr[j]):
count + = 1 ;
elif (arr[j] + arr[k] = = arr[i]):
count + = 1 ;
return count;
n = 4 ;
arr = [ 1 , 5 , 3 , 2 ];
print (countTriplets(arr, n));
|
C#
using System;
public class Gfg
{
static int countTriplets( int [] arr, int n)
{
int count = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
for ( int k = j + 1; k < n; k++) {
if (arr[i] + arr[j] == arr[k]) {
count++;
}
else if (arr[i] + arr[k] == arr[j]) {
count++;
}
else if (arr[j] + arr[k] == arr[i]) {
count++;
}
}
}
}
return count;
}
public static void Main( string [] args)
{
int n = 4;
int [] arr = { 1, 5, 3, 2 };
Console.Write(countTriplets(arr, n));
}
}
|
Javascript
function countTriplets(arr, n)
{
let count = 0;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
for (let k = j + 1; k < n; k++) {
if (arr[i] + arr[j] == arr[k]) {
count++;
}
else if (arr[i] + arr[k] == arr[j]) {
count++;
}
else if (arr[j] + arr[k] == arr[i]) {
count++;
}
}
}
}
return count;
}
let n = 4;
let arr = [1, 5, 3, 2 ];
document.write(countTriplets(arr, n));
|
Time Complexity: O(N3)
Auxiliary Space: O(1)
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 the 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 if the sum of the elements at those indices has a 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++
#include <bits/stdc++.h>
using namespace std;
int countTriplets( int arr[], int n){
int freq[100] = {0};
for ( int i=0; i < n; i++){
freq[arr[i]]++;
}
int count = 0;
for ( int i = 0;i < n; i++){
for ( int j = i+1; j < n; j++){
if (freq[arr[i] + arr[j]]){
count++;
}
}
}
return count;
}
int main()
{
int n = 4;
int arr[] = {1, 5, 3, 2};
cout << countTriplets(arr, n);
return 0;
}
|
Java
class GFG{
static int countTriplets( int arr[], int n){
int []freq = new int [ 100 ];
for ( int i = 0 ; i < n; i++){
freq[arr[i]]++;
}
int count = 0 ;
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;
}
public static void main(String[] args)
{
int n = 4 ;
int arr[] = { 1 , 5 , 3 , 2 };
System.out.print(countTriplets(arr, n));
}
}
|
Python 3
def countTriplets(arr, n):
freq = [ 0 for i in range ( 100 )]
for i in range (n):
freq[arr[i]] + = 1
count = 0
for i in range (n):
for j in range (i + 1 , n, 1 ):
if (freq[arr[i] + arr[j]]):
count + = 1
return count
if __name__ = = '__main__' :
n = 4
arr = [ 1 , 5 , 3 , 2 ]
print (countTriplets(arr, n))
|
C#
using System;
class GFG{
static int countTriplets( int []arr, int n){
int []freq = new int [100];
for ( int i = 0; i < n; i++){
freq[arr[i]]++;
}
int count = 0;
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;
}
public static void Main( string [] args)
{
int n = 4;
int []arr = {1, 5, 3, 2};
Console.WriteLine(countTriplets(arr, n));
}
}
|
Javascript
<script>
function countTriplets(arr, n){
let freq = new Uint8Array(100);
for (let i=0; i < n; i++){
freq[arr[i]]++;
}
let count = 0;
for (let i = 0;i < n; i++){
for (let j = i+1; j < n; j++){
if (freq[arr[i] + arr[j]]){
count++;
}
}
}
return count;
}
let n = 4;
let arr = [1, 5, 3, 2];
document.write(countTriplets(arr, n));
</script>
|
Performance Analysis:
- Time Complexity: O(N2).
- Auxiliary Space: O(N).
Share your thoughts in the comments
Please Login to comment...