JavaScript Program to Count triplets with sum smaller than a given value

We will be writing a JavaScript program to count the number of triplets with a sum smaller than a given value. This problem can be solved by sorting the array and using two pointers to check for the possible combinations. Firstly, we will sort the array in ascending order and then, for each element in the array, we will use two pointers to check for the triplets with a sum less than the given value. The number of such triplets will be the count we will be keeping track of.

Additionally, we will be updating the count and the pointers based on the sum of the triplet being less or equal to the given value. This way, we can efficiently solve the problem in O(n^2) time complexity. It is a very useful technique to keep in mind for future problems where we need to find the count of some combinations that satisfy a certain condition.

In the end, we will be returning the count of such triplets with a sum less than the given value.

Approach

• First, sort the given array of numbers in ascending order.

• Initialize three variables: left, right, and count.

• Then use the two pointers approach, left pointer starts from 0 and right pointer starts from end.

• For every iteration, calculate the sum of the current triplet (element pointed by left + element pointed by right + current element).

• If the sum is smaller than the given value, increment the count and the left pointer.

• If the sum is greater than the given value, decrement the right pointer. Repeat the process until the left pointer is less than the right pointer.

Example

Here is a complete example of a JavaScript program to count the number of triplets with a sum smaller than a given value −

function countTriplets(arr, sum) {
let count = 0;
arr.sort((a, b) => a - b);
// sorting the array in ascending order
for (let i = 0; i < arr.length - 2; i++) {
let left = i + 1;
let right = arr.length - 1;
while (left < right) {
if (arr[i] + arr[left] + arr[right] >= sum) {
right--;
} else {
count += right - left;
left++;
}
}
}
return count;
}
const arr = [5, 1, 3, 4, 7];
const sum = 12;
console.log(countTriplets(arr, sum));


Explanation

• The countTriplets function takes an array arr and a value sum as its parameters.

• The count variable is used to keep track of the number of triplets with a sum less than sum.

• The arr is sorted in ascending order using the sort function.

• The outer loop for (let i = 0; i < arr.length - 2; i++) iterates through the array, and the left and right pointers are initialized to the next index of i and the last index of the array, respectively.

• The while (left < right) loop continues until the left pointer is greater than or equal to the right pointer.

• The while (left < right) loop continues until the left pointer is greater than or equal to the right pointer.

• In each iteration of the while loop, the sum of arr[i], arr[left], and arr[right] is calculated. If this sum is greater than or equal to sum, then the right pointer is decremented. If the sum is less than sum, then the count is incremented by the number of remaining elements between the left and right pointers, and the left pointer is incremented.

• The function returns the count variable, which represents the number of triplets with a sum less than sum.