BackmediumArrays Cred

Galactic Resource Allocation Solution

Problem Statement

Given an array of non-negative integers, partition the resources 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.

Example 1
Input
[18, 22, 15, 11, 9, 6, 4]
Output
[18, 22, 15]

Explanation: Step-by-step: First, sort the array in descending order. Then, assign the first three elements to the first group, the next three elements to the second group, and the last element to the third group. The sum of the first group is 55, which is greater than the sum of the second group (20). The sum of the third group (18) is also greater than the sum of the second group (20).

Example 2
Input
[11, 9, 6, 8, 7, 5, 3, 2, 1]
Output
[11, 9, 6]

Explanation: Step-by-step: First, sort the array in descending order. Then, assign the first three elements to the first group, the next three elements to the second group, and the last element to the third group. The sum of the first group is 26, which is greater than the sum of the second group (26). To fix this, we need to adjust the assignment of elements to the groups so that the sum of the first group is greater than the sum of the second group.

Constraints

  • 1 <= resources.length <= 100
  • 1 <= resources[i] <= 1000
  • The number of groups is fixed at 3.
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free