
- 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
Dynamic Programming - Part sum of elements JavaScript
Suppose, we have an array of numbers like this −
const arr = [1, 2, 3, 4, 5];
Taking one element less each time, this array can be separated out like this −
[1, 2, 3, 4, 5] [2, 3, 4, 5] [3, 4, 5] [4, 5] [5] []
We are required to write a JavaScript function that takes in one such array. The function should separate out the array in the same way described above.
The function should then construct an array containing the respective sums of these parts and return that array.
Therefore, for this array, the output should look like −
const output = [15, 14, 12, 9, 5 0];
We will use Dynamic Programming to solve this problem. We will first calculate the sum of complete array in O(n) time, which eventually will become the first element of array. Then in another iteration, we will keep subtracting the corresponding elements to get the output array elements. This way we can solve this problem in O(n) time and O(1) space.
Example
const arr = [1, 2, 3, 4, 5]; const sumArray = (arr = []) => arr.reduce((a, b) => a + b, 0); const partialSum = (arr = []) => { let sum = sumArray(arr); const res = [sum]; for(let i = 0; i < arr.length; i++){ const el = arr[i]; sum -= el; res.push(sum); }; return res; }; console.log(partialSum(arr));
Output
This will produce the following output −
[ 15, 14, 12, 9, 5, 0 ]
- Related Articles
- Dynamic Programming in JavaScript
- Sum over Subsets - Dynamic Programming in C++
- Dynamic programming to check dynamic behavior of an array in JavaScript
- Dynamic Programming: Is second string subsequence of first JavaScript
- Dynamic Programming: return all matched data in JavaScript
- Introduction to Dynamic Programming
- In JavaScript, need to perform sum of dynamic array
- Attach event to dynamic elements in JavaScript?
- Bitmasking and Dynamic Programming in C++
- Cumulative sum of elements in JavaScript
- Absolute sum of array elements - JavaScript
- Maximum sum of nodes in Binary tree such that no two are adjacent | Dynamic Programming In C++
- Thrice sum of elements of array - JavaScript
- Difference Between Greedy Method and Dynamic Programming
- Sum of distinct elements of an array - JavaScript
