Reachable sum with reusable values and a huge target
Given a non-empty list nums of positive integers and a target k, return true if some multiset of values from nums sums to exactly k, and false otherwise. You may use each value any number of times. The list may contain duplicates.
Implement:
def subset_sum_equals_k(nums: list[int], k: int) -> bool: ...
Constraints
1 <= len(nums) <= 10001 <= nums[i] <= 10^4-
1 <= k <= 10^12. The target is far too large to loop over every sum from 0 tok.
Example 1
nums = [4, 6], k = 14 -> true (4 + 4 + 6)
nums = [4, 6], k = 15 -> false (every sum is even)
Example 2
nums = [5, 7], k = 23 -> false
nums = [5, 7], k = 24 -> true (5 + 5 + 7 + 7)
Follow-ups may ask you to return the multiset itself, answer many targets for the same list, or forbid reusing an element.
Reconstructed from the reported question; details may differ from what interviewers ask.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
