
- 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
Triplet with desired sum in JavaScript
We are required to write a JavaScript function that takes in an array of numbers as the first argument and a number as the second argument. The function should prepare and return an array of all such triplets (consecutive or nonconsecutive), that add up to the number specified by the second argument.
For example −
If the input array and the number are −
const arr = [4, 2, 0, 1, 2, 6, 8, 3, 2, 5]; const num = 8;
Then the output array should be −
const output = [ [ 2, 2, 4 ], [ 1, 3, 4 ], [ 0, 2, 6 ], [ 1, 2, 5 ] ];
Example
Following is the code −
const arr = [4, 2, 0, 1, 2, 6, 8, 3, 2, 5]; const num = 8; const tripletSum = (arr, num) => { if (arr.length === 3) { if (arr[0]+arr[1]+arr[2] === 0) { return [[arr[0],arr[1],arr[2]]]; }; }; const results = []; const hashMap = {}; for (var i=0; i<arr.length; i++) { for (var j=i+1; j<arr.length; j++) { for (var k=j+1; k<arr.length; k++) { if (arr[i]+arr[j]+arr[k] === num) { if (!hashMap[arr[i]*arr[j]*arr[k]]) { results.push([arr[i],arr[j],arr[k]]); results[results.length-1].sort(); hashMap[arr[i]*arr[j]*arr[k]] = true; } } } } } return results; }; console.log(tripletSum(arr, num));
Output
Following is the console output −
[ [ 2, 2, 4 ], [ 1, 3, 4 ], [ 0, 2, 6 ], [ 1, 2, 5 ] ]
- Related Articles
- Binary subarrays with desired sum in JavaScript
- Finding desired sum of elements in an array in JavaScript
- Check if string ends with desired character in JavaScript
- Maximum triplet sum in array in C++
- Check if a triplet with given sum exists in BST in Python
- Generating desired combinations in JavaScript
- JavaScript Program to Find a triplet that sum to a given value
- Checking for increasing triplet in JavaScript
- Finding three desired consecutive numbers in JavaScript
- Using operations to produce desired results in JavaScript
- JavaScript Program to Find a triplet such that sum of two equals to third element
- Finding desired numbers in a sorted array in JavaScript
- Find a triplet that sum to a given value in C++
- Find a triplet from three linked lists with a sum equal to a given number in C++
- Create Triplet Tuple in Java using with() method

Advertisements