BackmediumArraysPayPal

Maximum Alternating Sum Solution

Problem Statement

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.

Example 1
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.

Example 2
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.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Maximum Alternating Sum — Problem Statement & Solution Guide

ArraysMediumprefix sum and greedy
TimeO(n^3)
|
SpaceO(n)

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

Example 1

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.

Example 2

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

JavaScript Solution
Time: O(n^3)
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

PayPal

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.