Given an array of distinct integers and a sum value. Find count of triplets with sum smaller than given sum value. The expected Time Complexity is O(n2).
Examples:
Input : arr[] = {-2, 0, 1, 3}
sum = 2.
Output : 2
Explanation : Below are triplets with sum less than 2
(-2, 0, 1) and (-2, 0, 3)
Input : arr[] = {5, 1, 3, 4, 7}
sum = 12.
Output : 4
Explanation : Below are triplets with sum less than 12
(1, 3, 4), (1, 3, 5), (1, 3, 7) and
(1, 4, 5)
A Simple Solution is to run three loops to consider all triplets one by one. For every triplet, compare the sums and increment count if the triplet sum is smaller than the given sum.
C++
#include <bits/stdc++.h>
using namespace std;
int countTriplets( int arr[], int n, int sum)
{
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] < sum)
ans++;
}
}
return ans;
}
int main()
{
int arr[] = { 5, 1, 3, 4, 7 };
int n = sizeof arr / sizeof arr[0];
int sum = 12;
cout << countTriplets(arr, n, sum) << endl;
return 0;
}
|
C
#include <stdio.h>
int countTriplets( int arr[], int n, int sum)
{
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] < sum)
ans++;
}
}
return ans;
}
int main()
{
int arr[] = { 5, 1, 3, 4, 7 };
int n = sizeof arr / sizeof arr[0];
int sum = 12;
printf ( "%d\n" , countTriplets(arr, n, sum));
return 0;
}
|
Java
class Test
{
static int arr[] = new int []{ 5 , 1 , 3 , 4 , 7 };
static int countTriplets( int n, int sum)
{
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] < sum)
ans++;
}
}
return ans;
}
public static void main(String[] args)
{
int sum = 12 ;
System.out.println(countTriplets(arr.length, sum));
}
}
|
Python 3
def countTriplets(arr, n, sum ):
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] < sum ):
ans + = 1
return ans
arr = [ 5 , 1 , 3 , 4 , 7 ]
n = len (arr)
sum = 12
print (countTriplets(arr, n, sum ))
|
C#
using System;
class Test
{
static int [] arr = new int []{5, 1, 3, 4, 7};
static int countTriplets( int n, int sum)
{
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] < sum)
ans++;
}
}
return ans;
}
public static void Main()
{
int sum = 12;
Console.Write(countTriplets(arr.Length, sum));
}
}
|
Javascript
<script>
let arr = [5, 1, 3, 4, 7];
function countTriplets(n,sum)
{
let ans = 0;
for (let i = 0; i < n-2; i++)
{
for (let j = i + 1; j < n-1; j++)
{
for (let k = j + 1; k < n; k++)
if (arr[i] + arr[j] + arr[k] < sum)
ans++;
}
}
return ans;
}
let sum = 12;
document.write(countTriplets(arr.length, sum));
</script>
|
Output:
4
Time Complexity: O(n3)
Auxiliary Space: O(1)
An Efficient Solution can count triplets in O(n2) by sorting the array first, and then using method 1 of this post in a loop.
1) Sort the input array in increasing order.
2) Initialize result as 0.
3) Run a loop from i = 0 to n-2. An iteration of this loop finds all
triplets with arr[i] as first element.
a) Initialize other two elements as corner elements of subarray
arr[i+1..n-1], i.e., j = i+1 and k = n-1
b) Move j and k toward each other until they meet, i.e., while (j<k),
(i) If arr[i] + arr[j] + arr[k] >= sum
then k--
// Else for current i and j, there can (k-j) possible third elements
// that satisfy the constraint.
(ii) Else Do ans += (k - j) followed by j++
Below is the implementation of the above idea.
C++
#include<bits/stdc++.h>
using namespace std;
int countTriplets( int arr[], int n, int sum)
{
sort(arr, arr+n);
int ans = 0;
for ( int i = 0; i < n - 2; i++)
{
int j = i + 1, k = n - 1;
while (j < k)
{
if (arr[i] + arr[j] + arr[k] >= sum)
k--;
else
{
ans += (k - j);
j++;
}
}
}
return ans;
}
int main()
{
int arr[] = {5, 1, 3, 4, 7};
int n = sizeof arr / sizeof arr[0];
int sum = 12;
cout << countTriplets(arr, n, sum) << endl;
return 0;
}
|
Java
import java.util.Arrays;
class Test
{
static int arr[] = new int []{ 5 , 1 , 3 , 4 , 7 };
static int countTriplets( int n, int sum)
{
Arrays.sort(arr);
int ans = 0 ;
for ( int i = 0 ; i < n - 2 ; i++)
{
int j = i + 1 , k = n - 1 ;
while (j < k)
{
if (arr[i] + arr[j] + arr[k] >= sum)
k--;
else
{
ans += (k - j);
j++;
}
}
}
return ans;
}
public static void main(String[] args)
{
int sum = 12 ;
System.out.println(countTriplets(arr.length, sum));
}
}
|
Python3
def countTriplets(arr,n, sum ):
arr.sort()
ans = 0
for i in range ( 0 ,n - 2 ):
j = i + 1
k = n - 1
while (j < k):
if (arr[i] + arr[j] + arr[k] > = sum ):
k = k - 1
else :
ans + = (k - j)
j = j + 1
return ans
if __name__ = = '__main__' :
arr = [ 5 , 1 , 3 , 4 , 7 ]
n = len (arr)
sum = 12
print (countTriplets(arr, n, sum ))
|
C#
using System;
class GFG
{
static int []arr = new int []{5, 1, 3, 4, 7};
static int countTriplets( int n, int sum)
{
Array.Sort(arr);
int ans = 0;
for ( int i = 0; i < n - 2; i++)
{
int j = i + 1, k = n - 1;
while (j < k)
{
if (arr[i] + arr[j] + arr[k] >= sum)
k--;
else
{
ans += (k - j);
j++;
}
}
}
return ans;
}
public static void Main()
{
int sum = 12;
Console.Write(countTriplets(arr.Length, sum));
}
}
|
Javascript
<script>
let arr = [5, 1, 3, 4, 7];
function countTriplets(n,sum)
{
arr.sort( function (a,b){ return b-a});
let ans = 0;
for (let i = 0; i < n - 2; i++)
{
let j = i + 1, k = n - 1;
while (j < k)
{
if (arr[i] + arr[j] + arr[k] >= sum)
k--;
else
{
ans += (k - j);
j++;
}
}
}
return ans;
}
let sum = 12;
document.write(countTriplets(arr.length, sum));
</script>
|
Output:
4
Time Complexity: O(n2)
Auxiliary Space: O(1)
Thanks to Gaurav Ahirwar for suggesting this solution.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above