Count Triplets such that one of the numbers can be written as sum of the other two
Given an array A[] of N integers. The task is to find the number of triples (i, j, k) , where i, j, k are indices and (1 <= i < j < k <= N), such that in the set { , , } at least one of the numbers can be written as the sum of the other two.
Examples:
Input : A[] = {1, 2, 3, 4, 5}
Output : 4
The valid triplets are:
(1, 2, 3), (1, 3, 4), (1, 4, 5), (2, 3, 5)
Input : A[] = {1, 1, 1, 2, 2}
Output : 6
This is a counting problem. Let’s say f(x) represents the frequency of number in our array.
There exist four cases:
- All three numbers are equal to 0. The number of ways = f(0)C3 (where pCq is the number of ways of choosing q numbers from p numbers).
- One number is equal to 0, the other two are equal to some x > 0: f(0) * f(x)C2.
- Two numbers are equal to some x>0, the third is 2*x: f(x)C2 * f(2 * x).
- The three numbers are x, y and x + y, 0 < x, y: f(x) * f(y) * f(x + y).
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int countWays( int arr[], int n)
{
int max_val = 0;
for ( int i = 0; i < n; i++)
max_val = max(max_val, arr[i]);
int freq[max_val + 1]={0};
for ( int i = 0; i < n; i++)
freq[arr[i]]++;
int ans = 0;
ans += freq[0] * (freq[0] - 1) * (freq[0] - 2) / 6;
for ( int i = 1; i <= max_val; i++)
ans += freq[0] * freq[i] * (freq[i] - 1) / 2;
for ( int i = 1; 2 * i <= max_val; i++)
ans += freq[i] * (freq[i] - 1) / 2 * freq[2 * i];
for ( int i = 1; i <= max_val; i++) {
for ( int j = i + 1; i + j <= max_val; j++)
ans += freq[i] * freq[j] * freq[i + j];
}
return ans;
}
int main()
{
int arr[]={ 1, 2, 3, 4, 5 };
int n = sizeof (arr)/ sizeof ( int );
cout<<(countWays(arr, n));
return 0;
}
|
Java
class GFG {
static int countWays( int [] arr, int n)
{
int max_val = 0 ;
for ( int i = 0 ; i < n; i++)
max_val = Math.max(max_val, arr[i]);
int [] freq = new int [max_val + 1 ];
for ( int i = 0 ; i < n; i++)
freq[arr[i]]++;
int ans = 0 ;
ans += freq[ 0 ] * (freq[ 0 ] - 1 ) * (freq[ 0 ] - 2 ) / 6 ;
for ( int i = 1 ; i <= max_val; i++)
ans += freq[ 0 ] * freq[i] * (freq[i] - 1 ) / 2 ;
for ( int i = 1 ; 2 * i <= max_val; i++)
ans += freq[i] * (freq[i] - 1 ) / 2 * freq[ 2 * i];
for ( int i = 1 ; i <= max_val; i++) {
for ( int j = i + 1 ; i + j <= max_val; j++)
ans += freq[i] * freq[j] * freq[i + j];
}
return ans;
}
public static void main(String[] args)
{
int [] arr = new int [] { 1 , 2 , 3 , 4 , 5 };
int n = arr.length;
System.out.println(countWays(arr, n));
}
}
|
Python3
import math as mt
def countWays(arr, n):
max_val = 0
for i in range (n):
max_val = max (max_val, arr[i])
freq = [ 0 for i in range (max_val + 1 )]
for i in range (n):
freq[arr[i]] + = 1
ans = 0
ans + = (freq[ 0 ] * (freq[ 0 ] - 1 ) *
(freq[ 0 ] - 2 ) / / 6 )
for i in range ( 1 , max_val + 1 ):
ans + = (freq[ 0 ] * freq[i] *
(freq[i] - 1 ) / / 2 )
for i in range ( 1 , (max_val + 1 ) / / 2 ):
ans + = (freq[i] *
(freq[i] - 1 ) / / 2 * freq[ 2 * i])
for i in range ( 1 , max_val + 1 ):
for j in range (i + 1 , max_val - i + 1 ):
ans + = freq[i] * freq[j] * freq[i + j]
return ans
arr = [ 1 , 2 , 3 , 4 , 5 ]
n = len (arr)
print (countWays(arr, n))
|
C#
using System;
class GFG
{
static int countWays( int [] arr, int n)
{
int max_val = 0;
for ( int i = 0; i < n; i++)
max_val = Math.Max(max_val, arr[i]);
int [] freq = new int [max_val + 1];
for ( int i = 0; i < n; i++)
freq[arr[i]]++;
int ans = 0;
ans += freq[0] * (freq[0] - 1) *
(freq[0] - 2) / 6;
for ( int i = 1; i <= max_val; i++)
ans += freq[0] * freq[i] *
(freq[i] - 1) / 2;
for ( int i = 1;
2 * i <= max_val; i++)
ans += freq[i] * (freq[i] - 1) /
2 * freq[2 * i];
for ( int i = 1; i <= max_val; i++)
{
for ( int j = i + 1;
i + j <= max_val; j++)
ans += freq[i] * freq[j] *
freq[i + j];
}
return ans;
}
public static void Main()
{
int [] arr = { 1, 2, 3, 4, 5 };
int n = arr.Length;
Console.WriteLine(countWays(arr, n));
}
}
|
Javascript
<script>
function countWays(arr,n)
{
let max_val = 0;
for (let i = 0; i < n; i++)
max_val = Math.max(max_val, arr[i]);
let freq = new Array(max_val + 1);
for (let i=0;i<freq.length;i++)
{
freq[i]=0;
}
for (let i = 0; i < n; i++)
freq[arr[i]]++;
let ans = 0;
ans += freq[0] * (freq[0] - 1) * (freq[0] - 2) / 6;
for (let i = 1; i <= max_val; i++)
ans += freq[0] * freq[i] * (freq[i] - 1) / 2;
for (let i = 1; 2 * i <= max_val; i++)
ans += freq[i] * (freq[i] - 1) / 2 * freq[2 * i];
for (let i = 1; i <= max_val; i++) {
for (let j = i + 1; i + j <= max_val; j++)
ans += freq[i] * freq[j] * freq[i + j];
}
return ans;
}
let arr=[1, 2, 3, 4, 5];
let n = arr.length;
document.write(countWays(arr, n));
</script>
|
PHP
<?php
function countWays( $arr , $n )
{
$max_val = 0;
for ( $i = 0; $i < $n ; $i ++)
$max_val = max( $max_val , $arr [ $i ]);
$freq = array_fill (0, $max_val + 1, 0);
for ( $i = 0; $i < $n ; $i ++)
$freq [ $arr [ $i ]]++;
$ans = 0;
$ans += (int)( $freq [0] * ( $freq [0] - 1) *
( $freq [0] - 2) / 6);
for ( $i = 1; $i <= $max_val ; $i ++)
$ans += (int)( $freq [0] * $freq [ $i ] *
( $freq [ $i ] - 1) / 2);
for ( $i = 1; 2 * $i <= $max_val ; $i ++)
$ans += (int)( $freq [ $i ] * ( $freq [ $i ] - 1) / 2 *
$freq [2 * $i ]);
for ( $i = 1; $i <= $max_val ; $i ++)
{
for ( $j = $i + 1; $i + $j <= $max_val ; $j ++)
$ans += $freq [ $i ] * $freq [ $j ] *
$freq [ $i + $j ];
}
return $ans ;
}
$arr = array ( 1, 2, 3, 4, 5 );
$n = count ( $arr );
echo countWays( $arr , $n );
?>
|
Time complexity of the given C++ program is O(max_val^2 ), where max_val is the maximum value in the input array.
The space complexity of the program is O(max_val), as we are using an array of size max_val+1 to store the frequency of elements in the input array.
Last Updated :
05 Feb, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...