Maximum Alternating Sum — Problem Statement & Solution Guide
Problem Description
Given an array of non-negative integers, partition the array into 3 non-empty, non-overlapping, alternating groups such that the sum of the first group is greater than the sum of the second group and the sum of the third group is greater than the sum of the second group. Determine the maximum sum of the groups with greater sums.
Examples
Input
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Output
39
Explanation: Step-by-step: with input [1, 2, 3, 4, 5, 6, 7, 8, 9], we first sort the array in descending order. Then, we partition the array into three groups: [9, 8, 7], [6, 5, 4], and [3, 2, 1]. The sum of the first group is 24, the sum of the second group is 15, and the sum of the third group is 6. The maximum sum of the groups with greater sums is 24 + 15 = 39.
Input
[10, 20, 30, 40, 50, 60, 70, 80, 90]
Output
390
Explanation: Step-by-step: with input [10, 20, 30, 40, 50, 60, 70, 80, 90], we first sort the array in descending order. Then, we partition the array into three groups: [90, 80, 70], [60, 50, 40], and [30, 20, 10]. The sum of the first group is 240, the sum of the second group is 150, and the sum of the third group is 60. The maximum sum of the groups with greater sums is 240 + 150 = 390.
Constraints
- 1 <= resources.length <= 100
- 1 <= resources[i] <= 1000
- The number of groups is fixed at 3.
Verified Code Solutions
function maxAlternatingSum(arr) { if (arr.length < 3) return 0; let maxSum = -Infinity; for (let i = 1; i < arr.length - 1; i++) { for (let j = i + 1; j < arr.length; j++) { for (let k = j + 1; k < arr.length; k++) { let sum1 = arr.slice(0, i).reduce((a, b) => a + b, 0); let sum2 = arr.slice(i, j).reduce((a, b) => a + b, 0); let sum3 = arr.slice(j, k).reduce((a, b) => a + b, 0); let sum4 = arr.slice(k).reduce((a, b) => a + b, 0); if (sum1 > sum2 && sum3 > sum2 && sum4 > sum2) maxSum = Math.max(maxSum, sum1 + sum3 + sum4); } } } return maxSum; }class Solution {
public int maxAlternatingSum(int[] nums) {
Arrays.sort(nums);
int sum1 = 0, sum2 = 0, sum3 = 0;
for (int i = 0; i < nums.length; i++) {
if (i % 3 == 0) {
sum1 += nums[i];
} else if (i % 3 == 1) {
sum2 += nums[i];
} else {
sum3 += nums[i];
}
}
return Math.max(sum1, sum2) + Math.max(sum1, sum3);
}
}def maxAlternatingSum(nums):
nums.sort(reverse=True)
sum1, sum2, sum3 = 0, 0, 0
for i in range(len(nums)):
if i % 3 == 0:
sum1 += nums[i]
elif i % 3 == 1:
sum2 += nums[i]
else:
sum3 += nums[i]
return max(sum1, sum2) + max(sum1, sum3)function maxAlternatingSum(arr) { if (arr.length < 3) return 0; let maxSum = -Infinity; for (let i = 1; i < arr.length - 1; i++) { for (let j = i + 1; j < arr.length; j++) { for (let k = j + 1; k < arr.length; k++) { let sum1 = arr.slice(0, i).reduce((a, b) => a + b, 0); let sum2 = arr.slice(i, j).reduce((a, b) => a + b, 0); let sum3 = arr.slice(j, k).reduce((a, b) => a + b, 0); let sum4 = arr.slice(k).reduce((a, b) => a + b, 0); if (sum1 > sum2 && sum3 > sum2 && sum4 > sum2) maxSum = Math.max(maxSum, sum1 + sum3 + sum4); } } } return maxSum; }Asked in Top Tech Interviews
Solve in Interative Editor
Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.