Loading
Loading
A small cargo plane has a left hold and a right hold, and it only flies level when both carry exactly the same weight. Every crate must go into one hold or the other, and crates cannot be split.
Given the crate weights, return true if the crates can be divided into two groups of equal total weight.
crates = [4, 1, 5, 9, 3, 2]total = 24each hold needs 12reachable after 4, 1, 5: {0, 1, 4, 5, 6, 9, 10}sums some crates can makeafter 9: still {0, 1, 4, 5, 6, 9, 10}9 and 10 were already reachable; 13 and up exceed 12after 3: 12 = 9 + 3 is reachableleft hold: 9 and 3right hold: 4 + 1 + 5 + 2 = 12the rest balances automaticallytrueSign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(n × total) time · O(total) space
Input. Run your code to watch it work on this input, one line at a time.
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.
9 + 3 = 12 on one side and 4 + 1 + 5 + 2 = 12 on the other.
Example 2
The total, 9, is odd, so the holds can never match.