
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count Triplets with Sum Smaller Than a Given Value in JavaScript
In this article, we will learn to count triplets with a sum smaller than a given value in JavaScript. Triplets in an array whose sum is less than a provided number is a popular problem in competitive coding and algorithm design.
Problem Statement
Given an array of integers and a target sum, we need to find the number of triplets (a, b, c).
For example
Input
const arr = [5, 1, 3, 4, 7]; const sum = 12;Output
4
Explanation: The valid triplets are :
(1, 3, 5)
(1, 3, 4)
(1, 4, 5)
(1, 3, 7), all having a sum of less than 12.
Using Sorting and Two-Pointer Technique
This issue can be addressed by sorting the array and employing two pointers to determine the possible combinations. First, we sort the array in ascending order, and then for each element in the array, we employ two pointers to determine the triplets whose sum is less than the value. The number of such triplets will be the count we will be keeping track of them.
Approach
Following are the steps to count triplets with a sum smaller than a given value in JavaScript ?
- First, sort the given array of numbers in ascending order.
- Initialize three variables: left, right, and count.
- Then use the two-pointers approach, the left pointer starts from 0 and right pointer starts from the 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.
sorting the array in ascending order using the sort() method ?
arr.sort((a, b) => a - b);
Left pointer and right pointer ?
let left = i + 1; let right = arr.length - 1;
Reduce the sum by moving the right pointer in the if condition ?
if (arr[i]="" +="" arr[left]="" arr[right]="">= sum) { right--; }
All triplets between left and right are valid ?
else { count += right - left; left++; }
Example
Below is an example of counting 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 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));
Output
4
Time complexity: O(n^2) due to sorting and the two-pointer traversal.
Space complexity: O(1) as it operates in place without extra storage.
Conclusion
Applying sorting and the two-pointer technique, we effectively tally triplets whose sum is less than a specified value. This approach greatly enhances performance when compared to brute force, allowing it to handle larger inputs.