
- 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
Finding three elements with required sum in an array in JavaScript
We are required to write a JavaScript function that takes in an array of numbers as the first argument and a single number as the second argument. The function should then pick three such numbers from the array, (if they exist) whose sum is equal to the number specified by the second argument.
The function should finally return an array of arrays of all such triplets if they exist, an empty array otherwise.
For example −
If the input array and the number is −
const arr = [2, 5, 7, 8, 9, 11, 1, 6]; const sum = 22;
Then the output should be −
const output = [ [ 2, 9, 11 ], [ 5, 6, 11 ], [ 5, 8, 9 ], [ 6, 7, 9 ] ];
Example
The code for this will be −
const arr = [2, 5, 7, 8, 9, 11, 1, 6]; const sum = 22; const threeSum = (arr = [], sum) => { arr.sort((a,b) => a - b); const res = []; for(let i=0; i < arr.length - 2; i++){ if(arr[i] != arr[i-1]){ let left = i + 1; let right = arr.length - 1; while (left < right){ const curr = arr[i] + arr[left] + arr[right]; if (curr === sum){ res.push([arr[i], arr[left], arr[right]]); while(arr[left] == arr[left + 1]) left ++ while(arr[right] == arr[right - 1]) right -- // making sure our solution set does not contain duplicate res left ++; right --; } else if(curr < sum) { left ++ } else if(curr > sum){ right -- }; }; }; }; return res }; console.log(threeSum(arr, sum));
Output
And the output in the console will be −
[ [ 2, 9, 11 ], [ 5, 6, 11 ], [ 5, 8, 9 ], [ 6, 7, 9 ] ]
- Related Articles
- Finding desired sum of elements in an array in JavaScript
- Finding special kind of elements with in an array in JavaScript
- Finding sum of alternative elements of the array in JavaScript
- Finding sum of a range in an array JavaScript
- Finding the group with largest elements with same digit sum in JavaScript
- Finding upper elements in array in JavaScript
- Finding the product of array elements with reduce() in JavaScript
- Finding sum of all unique elements in JavaScript
- How to find a group of three elements in an array whose sum equals some target sum JavaScript
- Sum of distinct elements of an array in JavaScript
- Check if three consecutive elements in an array is identical in JavaScript
- Consecutive elements sum array in JavaScript
- Finding two closest elements to a specific number in an array using JavaScript
- Sum of distinct elements of an array - JavaScript
- Finding array intersection and including repeating elements in JavaScript

Advertisements