- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing

- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM

- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers

- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources

# 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.**

- Related Articles
- Count numbers (smaller than or equal to N) with given digit sum in C++
- Javascript Program to Count pairs with given sum
- JavaScript Program to Find all triplets with zero sum
- Print triplets with sum less than or equal to k in C Program
- Count triplets in a sorted doubly linked list whose sum is equal to a given value x in C++
- All unique triplets that sum up to a given value in C++
- Largest rectangle sum smaller than num in JavaScript
- Print all triplets with given sum in C++
- JavaScript Program to Find a triplet that sum to a given value
- C++ Program to count number of operations needed to place elements whose index is smaller than value
- Find all triplets in a list with given sum in Python
- Count number of triplets with product equal to given number in C++
- Print all Jumping Numbers smaller than or equal to a given value in C++
- Count number of triplets with product equal to given number with duplicates allowed in C++
- Count triplets in a sorted doubly linked list whose product is equal to a given value x in C++