
- 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
Find possible numbers in array that can sum to a target value 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 pick such elements from the array that when added gives the sum specified by the second argument of the array. The function should return an array of all such subarrays of numbers that when added gives the required sum.
Note that the order is not important and if needed we have the liberty to use one number more than once in order to generate the sum.
For example −
If the input array and the sum are −
const arr = [14, 6, 10]; const sum = 40;
Then the output should be −
const output = [ [ 14, 14, 6, 6 ], [ 14, 6, 10, 10 ], [ 6, 6, 6, 6, 6, 10 ], [ 10, 10, 10, 10 ] ];
Example
const arr = [14, 6, 10]; const sum = 40; const findSum = (arr, sum) => { const res = []; const search = (index, part = []) => { const s = part.reduce((a, b) => a + b, 0); if (s === sum){ res.push(part) }; if (s >= sum || index >= arr.length){ return; }; search(index, part.concat(arr[index])); search(index + 1, part); }; search(0); return res; } console.log(findSum(arr, sum));
Output
This will produce the following output −
[ [ 14, 14, 6, 6 ], [ 14, 6, 10, 10 ], [ 6, 6, 6, 6, 6, 10 ], [ 10, 10, 10, 10 ] ]
- Related Articles
- Find all pairs that sum to a target value in JavaScript
- How to find a group of three elements in an array whose sum equals some target sum JavaScript
- Can form target array from source array JavaScript
- JavaScript Program to Find a triplet that sum to a given value
- Finding sum of remaining numbers to reach target average using JavaScript
- Print all possible expressions that evaluate to a target in C++
- Any possible combination to add up to target in JavaScript
- Converting array of Numbers to cumulative sum array in JavaScript
- Program to find number of distinct quadruple that forms target sum in python
- Sum of Mutated Array Closest to Target in C++
- Number of Submatrices That Sum to Target in C++
- C++ program to find maximum possible value of XORed sum
- Sum all duplicate value in array - JavaScript
- Find a triplet that sum to a given value in C++
- JavaScript group array - find the sets of numbers that can be traveled to using the edges defined

Advertisements