
- 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
Ways to get to a specific sum in JavaScript
Problem
We are required to write a JavaScript function that takes in an array of Integers, arr, as the first argument and a single integer, target, as the second argument.
For each Integer in the array, our function can either assign ‘+’ or ‘-’ to it.
Our function should find out how many ways in total exist to assign ‘+’, ‘-’ to make the sum of integers of the array equal to the target sum, target.
For example, if the input to the function is −
const arr = [1, 1, 1, 1, 1]; const target = 3;
Then the output should be −
const output = 5;
Output Explanation:
Because the 5 ways are −
-1+1+1+1+1 = 3 +1-1+1+1+1 = 3 +1+1-1+1+1 = 3 +1+1+1-1+1 = 3 +1+1+1+1-1 = 3
Example
The code for this will be −
const arr = [1, 1, 1, 1, 1]; const target = 3; const waysToSum = (arr = [], target = 1) => { const map = {}; const find = (arr, target, i) => { let val = i + '->' + target; if(map[val] !== undefined){ return map[val]; }; if(i === 0){ if (target === 0 && arr[0] === 0) { return 2 } return arr[0] === target || arr[0] === -target ? 1 : 0 }; map[val] = find(arr, target + arr[i], i - 1) + find(arr, target - arr[i], i - 1); return map[val] }; return find(arr, target, arr.length-1) }; console.log(waysToSum(arr, target));
Output
And the output in the console will be −
5
- Related Articles
- How to get the sum of a specific column of a dataframe in Pandas Python?
- Get the Triplets in an Array Whose Sum is Equal to a Specific Number in Java
- Ways to create a Set in JavaScript?
- Get the sum only from specific cells in MySQL?
- Various ways to define a variable in JavaScript
- Best way to find two numbers in an array whose sum is a specific number with JavaScript?
- MongoDB query to sum specific fields
- Studying in Germany and Ways to Get There
- How to use Get-ChildItem in PowerShell to filter a specific extension?
- Count ways to express a number as sum of powers in C++
- MongoDB query to get a specific number of items
- MySQL query to get a specific row from rows
- How to get the sum of the powers of all numbers in JavaScript?
- Push specific elements to last in JavaScript
- Python Program to Filter Rows with a specific Pair Sum

Advertisements