Subset Sum and Equal Partition

medium
Unit 12 · Dynamic Programming II0/3 to milestone

Given non-negative integers, can you split them into two groups with equal sums?

Implement:

  • can_partition(nums)
  • subset_with_sum(nums, target): a sorted list of indices whose values sum to target, or None. Any valid subset is fine.
  • min_partition_difference(nums): the smallest possible |sum(A) - sum(B)| over all splits into two groups

Use the DP over reachable sums. A neat trick in Python: store the reachable set as the bits of one big integer, reach |= reach << x.

#dynamic-programming#bitsets2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Subset Sum and Equal Partition · TechGradify