Two Equal Delivery Vans

Medium · 2-D & String DP · about 20 min

A depot has a list of parcel weights. Every parcel must go into one of two vans, and the two vans must end up carrying exactly the same total weight.

Return True / true if that is possible, otherwise False / false. An empty list counts as possible (both vans carry 0).

  • 0 <= len(weights) <= 200, each weight is a whole number from 1 to 1000.
  • Parcels cannot be split.

Examples:

can_split_evenly([3, 1, 4, 2, 2]) → True (4 + 2 and 3 + 1 + 2)

can_split_evenly([2, 2, 3, 5]) → False (the total 12 is even, but no group weighs 6)

Hints

  • If the total weight is odd, the answer is already no.
  • Otherwise the real question is whether some parcels add up to exactly half the total.
  • Keep a yes/no list for every weight from 0 to half. Only 0 is reachable at the start.
  • For each parcel, update the weights from high to low, so the same parcel is never counted twice.

Learn the pattern first: the 2-D & String DP lesson.

More 2-D & String DP problems