All problems
0197MediumArrayDynamic ProgrammingKnapsack Problem0-1 Knapsack

Even Split of Crate Loads

Tracked in this browser only
Write code

Trains the technique from

LeetCode 416Partition Equal Subset Sum

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A dispatcher has two identical trailers standing at a loading bay and an integer array weights, where weights[i] is the weight of the i-th crate.

Every crate has to be rolled onto exactly one of the two trailers, and no crate may be left on the bay or cut apart. The bay foreman wants the two trailers to leave carrying the same total weight.

Return true if such a hand-out exists and false if it does not. A trailer is allowed to end up with any number of crates, and the crates do not have to be handed out in the order they are listed.

Examples

Example 1

Input
weights = [6, 2, 4, 8]
Output
true

Rolling the 6 and the 4 onto one trailer leaves the 2 and the 8 for the other, and both leave with 10.

Example 2

Input
weights = [7, 3, 9]
Output
false

The crates weigh 19 in total, an odd figure, so no hand-out can leave the two trailers level.

Example 3

Input
weights = [3, 3, 3, 4, 5]
Output
true

The three crates of weight 3 go on one trailer and the 4 and the 5 go on the other, and each trailer leaves with 9.

Constraints

  • 1 <= weights.length <= 200
  • 1 <= weights[i] <= 100

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def can_split_crates(weights: list[int]) -> bool:
Java
public boolean canSplitCrates(int[] weights)
September 7
Apply