Greatest sum of average of partitions in JavaScript

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.

Problem Statement

Given an array and a maximum number of partitions, find the partition that maximizes the sum of averages of all groups.

Input:

const arr = [10, 2, 3, 4, 10];
const num = 3;

Expected Output:

23

Explanation:

If we partition the array like this: [10], [2, 3, 4], [10]

The sum of averages will be: 10 + (2+3+4)/3 + 10 = 10 + 3 + 10 = 23

Solution Using Dynamic Programming

This problem can be solved using dynamic programming. We'll use a recursive approach with memoization to find the optimal partitioning.

const arr = [10, 2, 3, 4, 10];
const num = 3;

const greatestSum = (arr, k) => {
    const n = arr.length;
    
    // Prefix sum for quick range sum calculation
    const prefixSum = new Array(n + 1).fill(0);
    for (let i = 0; i < n; i++) {
        prefixSum[i + 1] = prefixSum[i] + arr[i];
    }
    
    // Helper function to get sum of subarray
    const getSum = (start, end) => {
        return prefixSum[end + 1] - prefixSum[start];
    };
    
    // DP table: dp[i][j] = maximum sum using at most j groups for first i elements
    const dp = Array(n + 1).fill(null).map(() => Array(k + 1).fill(-Infinity));
    
    // Base case: 0 elements with 0 groups = 0
    dp[0][0] = 0;
    
    for (let i = 1; i <= n; i++) {
        for (let groups = 1; groups <= Math.min(i, k); groups++) {
            // Try all possible positions for the last group
            for (let start = groups - 1; start < i; start++) {
                if (dp[start][groups - 1] !== -Infinity) {
                    const groupSum = getSum(start, i - 1);
                    const groupAvg = groupSum / (i - start);
                    dp[i][groups] = Math.max(dp[i][groups], dp[start][groups - 1] + groupAvg);
                }
            }
        }
    }
    
    // Find maximum among all possible number of groups
    let result = -Infinity;
    for (let groups = 1; groups <= k; groups++) {
        result = Math.max(result, dp[n][groups]);
    }
    
    return result;
};

console.log(greatestSum(arr, num));
23

Step-by-Step Breakdown

1. Prefix Sum: We create a prefix sum array to quickly calculate the sum of any subarray.

2. Dynamic Programming: dp[i][j] represents the maximum sum of averages using at most j groups for the first i elements.

3. Recurrence Relation: For each position and number of groups, we try all possible starting positions for the last group.

Alternative Recursive Solution

const greatestSumRecursive = (arr, k) => {
    const memo = new Map();
    
    const solve = (index, groupsLeft) => {
        if (index === arr.length) {
            return groupsLeft === 0 ? 0 : -Infinity;
        }
        
        if (groupsLeft === 0) {
            return -Infinity;
        }
        
        const key = `${index}-${groupsLeft}`;
        if (memo.has(key)) {
            return memo.get(key);
        }
        
        let maxSum = -Infinity;
        let currentSum = 0;
        
        // Try all possible end positions for current group
        for (let end = index; end < arr.length; end++) {
            currentSum += arr[end];
            const groupAvg = currentSum / (end - index + 1);
            const remaining = solve(end + 1, groupsLeft - 1);
            
            if (remaining !== -Infinity) {
                maxSum = Math.max(maxSum, groupAvg + remaining);
            }
        }
        
        memo.set(key, maxSum);
        return maxSum;
    };
    
    return solve(0, k);
};

// Test with same input
console.log(greatestSumRecursive([10, 2, 3, 4, 10], 3));
23

Time Complexity

The time complexity is O(n² × k) where n is the array length and k is the maximum number of partitions. The space complexity is O(n × k) for the DP table.

Conclusion

This problem demonstrates the power of dynamic programming for optimization problems. The key insight is to build solutions incrementally while considering all possible ways to form the last group.

Updated on: 2026-03-15T23:19:00+05:30

186 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements