Count number of triplets in an array having sum in the range [a, b]
Last Updated :
11 Aug, 2021
Given an array of distinct integers and a range [a, b], the task is to count the number of triplets having a sum in the range [a, b].
Examples:
Input : arr[] = {8, 3, 5, 2}
range = [7, 11]
Output : 1
There is only one triplet {2, 3, 5}
having sum 10 in range [7, 11].
Input : arr[] = {2, 7, 5, 3, 8, 4, 1, 9}
range = [8, 16]
Output : 36
A naive approach is to run three loops to consider all the triplets one by one. Find the sum of each triplet and increment the count if the sum lies in a given range [a, b].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countTriplets( int arr[], int n, int a, int b)
{
int ans = 0;
for ( int i = 0; i < n - 2; i++) {
for ( int j = i + 1; j < n - 1; j++) {
for ( int k = j + 1; k < n; k++)
if (arr[i] + arr[j] + arr[k] >= a
&& arr[i] + arr[j] + arr[k] <= b)
ans++;
}
}
return ans;
}
int main()
{
int arr[] = { 2, 7, 5, 3, 8, 4, 1, 9 };
int n = sizeof arr / sizeof arr[0];
int a = 8, b = 16;
cout << countTriplets(arr, n, a, b) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static int countTriplets( int []arr, int n,
int a, int b)
{
int ans = 0 ;
for ( int i = 0 ; i < n - 2 ; i++)
{
for ( int j = i + 1 ; j < n - 1 ; j++)
{
for ( int k = j + 1 ; k < n; k++)
{
if (arr[i] + arr[j] + arr[k] >= a &&
arr[i] + arr[j] + arr[k] <= b)
{ans++;}
}
}
}
return ans;
}
public static void main(String[] args)
{
int [] arr = { 2 , 7 , 5 , 3 , 8 , 4 , 1 , 9 };
int n = arr.length;
int a = 8 , b = 16 ;
System.out.println( "" + countTriplets(arr, n,
a, b));
}
}
|
Python3
def countTriplets(arr, n, a, b):
ans = 0
for i in range ( 0 , n - 2 ):
for j in range (i + 1 , n - 1 ):
for k in range (j + 1 , n):
if ((arr[i] + arr[j] + arr[k] > = a)
and (arr[i] + arr[j] + arr[k] < = b)):
ans + = 1
return ans
if __name__ = = "__main__" :
arr = [ 2 , 7 , 5 , 3 , 8 , 4 , 1 , 9 ]
n = len (arr)
a = 8 ; b = 16
print (countTriplets(arr, n, a, b))
|
C#
using System;
class GFG
{
public static int countTriplets( int []arr, int n,
int a, int b)
{
int ans = 0;
for ( int i = 0;
i < n - 2; i++)
{
for ( int j = i + 1;
j < n - 1; j++)
{
for ( int k = j + 1;
k < n; k++)
{
if (arr[i] + arr[j] + arr[k] >= a &&
arr[i] + arr[j] + arr[k] <= b)
{ans++;}
}
}
}
return ans;
}
public static void Main()
{
int [] arr = {2, 7, 5, 3, 8, 4, 1, 9};
int n = arr.Length;
int a = 8, b = 16;
Console.WriteLine( "" + countTriplets(arr, n,
a, b));
}
}
|
PHP
<?php
function countTriplets( $arr , $n , $a , $b )
{
$ans = 0;
for ( $i = 0; $i < $n - 2; $i ++)
{
for ( $j = $i + 1; $j < $n - 1; $j ++)
{
for ( $k = $j + 1; $k < $n ; $k ++)
if ( $arr [ $i ] + $arr [ $j ] + $arr [ $k ] >= $a &&
$arr [ $i ] + $arr [ $j ] + $arr [ $k ] <= $b )
$ans ++;
}
}
return $ans ;
}
$arr = array ( 2, 7, 5, 3, 8, 4, 1, 9 );
$n = sizeof( $arr );
$a = 8; $b = 16;
echo countTriplets( $arr , $n , $a , $b ) . "\n" ;
?>
|
Javascript
<script>
function countTriplets( arr, n, a, b)
{
var ans = 0;
for ( var i = 0; i < n - 2; i++) {
for ( var j = i + 1; j < n - 1; j++) {
for ( var k = j + 1; k < n; k++)
if (arr[i] + arr[j] + arr[k] >= a
&& arr[i] + arr[j] + arr[k] <= b)
ans++;
}
}
return ans;
}
var arr = [ 2, 7, 5, 3, 8, 4, 1, 9 ];
var n = arr.length;
var a = 8, b = 16;
document.write( countTriplets(arr, n, a, b) );
</script>
|
Time complexity: O(n3)
Auxiliary Space: O(1)
An efficient solution is to first find the count of triplets having a sum less than or equal to upper limit b in the range [a, b]. This count of triplets will also include triplets having a sum less than the lower limit a. Subtract the count of triplets having a sum less than a. The final result is the count of triplets having a sum in the range [a, b].
The algorithm is as follows:
- Find count of triplets having a sum less than or equal to b. Let this count be x.
- Find count of triplets having a sum less than a. Let this count be y.
- Final result is x-y.
To find the count of triplets having a sum less than or equal to the given value, refer Count triplets with sum smaller than a given value
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countTripletsLessThan( int arr[], int n, int val)
{
sort(arr, arr + n);
int ans = 0;
int j, k;
int sum;
for ( int i = 0; i < n - 2; i++) {
j = i + 1;
k = n - 1;
while (j != k) {
sum = arr[i] + arr[j] + arr[k];
if (sum > val)
k--;
else {
ans += (k - j);
j++;
}
}
}
return ans;
}
int countTriplets( int arr[], int n, int a, int b)
{
int res;
res = countTripletsLessThan(arr, n, b) -
countTripletsLessThan(arr, n, a - 1);
return res;
}
int main()
{
int arr[] = { 2, 7, 5, 3, 8, 4, 1, 9 };
int n = sizeof arr / sizeof arr[0];
int a = 8, b = 16;
cout << countTriplets(arr, n, a, b) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static int countTripletsLessThan( int []arr,
int n, int val)
{
Arrays.sort(arr);
int ans = 0 ;
int j, k;
int sum;
for ( int i = 0 ; i < n - 2 ; i++)
{
j = i + 1 ;
k = n - 1 ;
while (j != k)
{
sum = arr[i] + arr[j] + arr[k];
if (sum > val)
k--;
else
{
ans += (k - j);
j++;
}
}
}
return ans;
}
public static int countTriplets( int arr[], int n,
int a, int b)
{
int res;
res = countTripletsLessThan(arr, n, b) -
countTripletsLessThan(arr, n, a - 1 );
return res;
}
public static void main(String[] args)
{
int [] arr = { 2 , 7 , 5 , 3 ,
8 , 4 , 1 , 9 };
int n = arr.length;
int a = 8 , b = 16 ;
System.out.println( "" + countTriplets(arr, n,
a, b));
}
}
|
Python3
def countTripletsLessThan(arr, n, val):
arr.sort()
ans = 0
j = 0 ; k = 0
sum = 0
for i in range ( 0 ,n - 2 ):
j = i + 1
k = n - 1
while j ! = k :
sum = arr[i] + arr[j] + arr[k]
if sum > val:
k - = 1
else :
ans + = (k - j)
j + = 1
return ans
def countTriplets(arr, n, a, b):
res = 0
res = (countTripletsLessThan(arr, n, b) -
countTripletsLessThan(arr, n, a - 1 ))
return res
if __name__ = = "__main__" :
arr = [ 2 , 7 , 5 , 3 , 8 , 4 , 1 , 9 ]
n = len (arr)
a = 8 ; b = 16
print (countTriplets(arr, n, a, b))
|
C#
using System;
class GFG
{
public static int countTripletsLessThan( int [] arr,
int n, int val)
{
Array.Sort(arr);
int ans = 0;
int j, k;
int sum;
for ( int i = 0; i < n - 2; i++)
{
j = i + 1;
k = n - 1;
while (j != k)
{
sum = arr[i] + arr[j] + arr[k];
if (sum > val)
k--;
else
{
ans += (k - j);
j++;
}
}
}
return ans;
}
public static int countTriplets( int [] arr, int n,
int a, int b)
{
int res;
res = countTripletsLessThan(arr, n, b) -
countTripletsLessThan(arr, n, a - 1);
return res;
}
public static void Main()
{
int [] arr = {2, 7, 5, 3,
8, 4, 1, 9};
int n = arr.Length;
int a = 8, b = 16;
Console.WriteLine( "" + countTriplets(arr, n,
a, b));
}
}
|
PHP
<?php
function countTripletsLessThan( $arr , $n , $val )
{
sort( $arr );
$ans = 0;
$sum ;
for ( $i = 0; $i < $n - 2; $i ++)
{
$j = $i + 1;
$k = $n - 1;
while ( $j != $k )
{
$sum = $arr [ $i ] + $arr [ $j ] + $arr [ $k ];
if ( $sum > $val )
$k --;
else
{
$ans += ( $k - $j );
$j ++;
}
}
}
return $ans ;
}
function countTriplets( $arr , $n , $a , $b )
{
$res ;
$res = countTripletsLessThan( $arr , $n , $b ) -
countTripletsLessThan( $arr , $n , $a - 1);
return $res ;
}
$arr = array ( 2, 7, 5, 3, 8, 4, 1, 9 );
$n = sizeof( $arr );
$a = 8;
$b = 16;
echo countTriplets( $arr , $n , $a , $b ), "\n" ;
?>
|
Javascript
<script>
function countTripletsLessThan(arr, n, val) {
arr.sort();
var ans = 0;
var j, k;
var sum;
for ( var i = 0; i < n - 2; i++) {
j = i + 1;
k = n - 1;
while (j != k) {
sum = arr[i] + arr[j] + arr[k];
if (sum > val) k--;
else {
ans += k - j;
j++;
}
}
}
return ans;
}
function countTriplets(arr, n, a, b) {
var res;
res =
countTripletsLessThan(arr, n, b) -
countTripletsLessThan(arr, n, a - 1);
return res;
}
var arr = [2, 7, 5, 3, 8, 4, 1, 9];
var n = arr.length;
var a = 8,
b = 16;
document.write( "" + countTriplets(arr, n, a, b));
</script>
|
Time complexity: O(n2)
Auxiliary space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...