- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Greatest sum of average of partitions in JavaScript
Problem
We are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument, and a number, num, (num <= size of arr), as the second argument.
Our function should partition the array arr into at most num adjacent (non-empty) groups in such a way that we leave no element behind.
From all such partitions, our function should pick that partition where the sum of averages of all the groups is the greatest.
And lastly we should return this greatest sum.
For example, if the input to the function is
Input
const arr = [10, 2, 3, 4, 10]; const num = 3;
Output
const output = 23;
Output Explanation
Because if we partition the array like this −
[10], [2, 3, 4], [10]
The sum of averages will be −
10 + (9)/3 + 10 = 23
Which is the greatest among all partitions.
Example
Following is the code −
const arr = [10, 2, 3, 4, 10]; const num = 3; const greatestSum = (arr, num) => { const sum = (arr = []) => arr.reduce((acc, num) => acc + num, 0) let matrix = new Array(num + 1).fill(0).map(() => new Array(arr.length + 1).fill(0)) for (let index = arr.length; index >= 0; index--) { const current = new Array(num + 1).fill(0).map(() => new Array(arr.length + 1).fill(0)) for (let currentK = num; currentK >= 0; currentK--) { for (let count = arr.length - 1; count >= 0; count--) { if (index === arr.length && currentK === num) { current[currentK][count] = 0 } else if (index < arr.length && currentK < num) { current[currentK][count] = Math.max( matrix[currentK][count + 1],matrix[currentK + 1][0] + sum(arr.slice(index - count, index + 1)) / (count + 1) ) } else { current[currentK][count] = -Infinity } } } matrix = current } return matrix[0][0] } console.log(greatestSum(arr, num));
Output
23
- Related Articles
- Greatest sum and smallest index difference in JavaScript
- Finding sum of remaining numbers to reach target average using JavaScript
- Greatest digit of a number in JavaScript
- Minimum partitions of maximum size 2 and sum limited by given value in C++
- Cumulative average of pair of elements in JavaScript
- Find the greatest product of three numbers in JavaScript
- Maximum average sum partition of an array in C++
- Calculating average of an array in JavaScript
- Return indexes of greatest values in an array in JavaScript
- Greatest Sum Divisible by Three in C++
- Program to check whether we can partition a list with k-partitions of equal sum in C++
- Calculating average of a sliding window in JavaScript
- Program to check whether we can make group of two partitions with equal sum or not in Python?
- Maximum average of a specific length of subarray in JavaScript
- Realtime moving average of an array of numbers in JavaScript

Advertisements