C++ Program to Count triplets with sum smaller than a given value
Last Updated :
15 Feb, 2023
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++
// A Simple C++ program to count triplets with sum smaller
// than a given value
#include<bits/stdc++.h>
using namespace std;
int countTriplets(int arr[], int n, int sum)
{
// Initialize result
int ans = 0;
// Fix the first element as A[i]
for (int i = 0; i < n-2; i++)
{
// Fix the second element as A[j]
for (int j = i+1; j < n-1; j++)
{
// Now look for the third number
for (int k = j+1; k < n; k++)
if (arr[i] + arr[j] + arr[k] < sum)
ans++;
}
}
return ans;
}
// Driver program
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;
}
Output:
4
Time Complexity: O(n3)
Auxiliary Space: O(1)
As constant extra space is used. 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= 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++
// C++ program to count triplets with sum smaller than a given value
#include<bits/stdc++.h>
using namespace std;
int countTriplets(int arr[], int n, int sum)
{
// Sort input array
sort(arr, arr+n);
// Initialize result
int ans = 0;
// Every iteration of loop counts triplet with
// first element as arr[i].
for (int i = 0; i < n - 2; i++)
{
// Initialize other two elements as corner elements
// of subarray arr[j+1..k]
int j = i + 1, k = n - 1;
// Use Meet in the Middle concept
while (j < k)
{
// If sum of current triplet is more or equal,
// move right corner to look for smaller values
if (arr[i] + arr[j] + arr[k] >= sum)
k--;
// Else move left corner
else
{
// This is important. For current i and j, there
// can be total k-j third elements.
ans += (k - j);
j++;
}
}
}
return ans;
}
// Driver program
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;
}
Output:
4
Time Complexity: O(n2)
Auxiliary Space: O(1)
As constant extra space is used.
Please refer complete article on Count triplets with sum smaller than a given value for more details!
Similar Reads
C++ Program to Find a triplet such that sum of two equals to third element Write a C++ program for a given array of integers, you have to find three numbers such that the sum of two elements equals the third element.Examples: Input: {5, 32, 1, 7, 10, 50, 19, 21, 2}Output: 21, 2, 19 Input: {5, 32, 1, 7, 10, 50, 19, 21, 0}Output: no such triplet exist Question source: Arcesi
6 min read
C++ Program For Finding A Triplet From Three Linked Lists With Sum Equal To A Given Number Given three linked lists, say a, b and c, find one node from each list such that the sum of the values of the nodes is equal to a given number. For example, if the three linked lists are 12->6->29, 23->5->8, and 90->20->59, and the given number is 101, the output should be triple "
4 min read
Count of even sum triplets in the array for Q range queries Given an array arr[] of size N and Q queries of the form (L, R), the task is to count number of triplets with even sum for the elements in the range L and R for each query. Examples: Input: N = 6, arr[ ] = {1, 2, 3, 4, 5, 6}, Q[ ] = {{1, 3}, {2, 5}}Output: 1 2Explanation:For Query (1, 3): only tripl
9 min read
C++ Program to Count Inversions of size three in a given array Given an array arr[] of size n. Three elements arr[i], arr[j] and arr[k] form an inversion of size 3 if a[i] > a[j] >a[k] and i Example :Â Â Input: {8, 4, 2, 1} Output: 4 The four inversions are (8,4,2), (8,4,1), (4,2,1) and (8,2,1). Input: {9, 6, 4, 5, 8} Output: 2 The two inversions are {9, 6, 4} a
4 min read
Count of numbers from range [L, R] whose sum of digits is Y | Set 2 Given three positive integers L, R and Y, the task is to count the numbers in the range [L, R] whose sum of digits is equal to Y Examples: Input: L = 500, R = 1000, Y = 6Output: 3Explanation: Numbers in the range [500, 600] whose sum of digits is Y(= 6) are: 501 = 5 + 0 + 1 = 6 510 = 5 + 1 + 0 = 6 6
15+ min read