Loading...
You are given an integer array nums, where every element is a power of two (1, 2, 4, ..., 2^30), and an integer target.
Select a subset of the elements of nums, using each element at most once, whose sum is exactly target.
Return the minimum number of elements in such a subset, or -1 if no subset of nums sums to target.
Input: nums = [4,8,2,1], target = 13
Output: 3
Explanation: 8 + 4 + 1 = 13 uses three elements; no two elements of nums sum to 13.
Input: nums = [2,2,2], target = 6
Output: 3
Explanation: All three elements are needed: 2 + 2 + 2 = 6.
Input: nums = [8,4], target = 5
Output: -1
Explanation: The possible sums are 4, 8, and 12, so 5 is unreachable.
nums.length ≤105nums[i] is a power of two: nums[i] ∈{1,2,4,…,230}target ≤109Click "Run" to test with sample cases or "Submit" to run all tests.