Modules on rockets
- O(2ⁿ) · Legendary
- Full plan
- Python
- JavaScript
- recursion
- lists
- booleans
- division
Problem
The space station will be assembled in orbit, and its modules go up on several rockets that launch together. Svetlana Orlova, the mission chief, has one condition: every rocket must carry exactly the same mass, not a kilo more and not a kilo less.
Write a function that takes modules, a list with the mass of each module (positive whole numbers, at least one, repeats allowed), and rockets, a whole number from 1 up. Each module goes whole on a single rocket, and all of them have to go up. Return true if they can be split so that every rocket carries the same mass, and false if not.
With [9, 3, 4, 3, 4, 1, 6] and 3 rockets it can be done: 9 and 1 on one, 3, 3 and 4 on another, and 4 and 6 on the last; each one carries 10. With [2, 2, 2, 3] and 3 rockets each one would have to carry 3, but a 2 has nothing to be completed with: it can't be done.
Examples
The example
[9, 3, 4, 3, 4, 1, 6], 3 → true
A module has nothing to be completed with
[2, 2, 2, 3], 3 → false
Two rockets
[3, 3, 2, 2, 2], 2 → true
The mass doesn't divide evenly
[4, 5, 6], 2 → false
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def can_split(modules, rockets):
passJavaScript
function canSplit(modules, rockets) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.