Selection proof: Positive-ink invoice
Inputs
readings is a list of integers (numbers with no decimal part). It may be empty, and numbers may repeat unless task 1 says otherwise.
Your tasks
- Create a list
subsetTotals. Choose any positions fromreadings, or choose none. Add the numbers at those positions. List every different total you can make, from smallest to largest. Choosing no positions gives a total of 0. Each position can be chosen at most once. Equal numbers at different positions are separate choices, but each total appears only once insubsetTotals. - Add only the numbers in
subsetTotalsthat are greater than 0. Store the sum inpositiveTotal. - Complete
selectionProofPositiveInkInvoiceso it returnspositiveTotal. Return it, do not print it.
Example
If readings is [7, 7, -8, 8, 9], then subsetTotals is [-8, -1, 0, 1, 6, 7, 8, 9, 14, 15, 16, 17, 22, 23, 24, 31] and selectionProofPositiveInkInvoice returns 193.
Rules
- Skip 0 and negative numbers.
- If no number is greater than 0, return 0.
Test data
TEST DATA 1: readings is [-10, 14, 14, 0, 11].
TEST DATA 2: readings is [7, 7, -3, 7].
Examples
Input: readings = [-10,14,14,0,11] Output: 184
subsetTotals is [-10, 0, 1, 4, 11, 14, 15, 18, 25, 28, 29, 39]. Keep [1, 4, 11, 14, 15, 18, 25, 28, 29, 39] and add those numbers. Return 184.
Input: readings = [7,7,-3,7] Output: 75
subsetTotals is [-3, 0, 4, 7, 11, 14, 18, 21]. Keep [4, 7, 11, 14, 18, 21] and add those numbers. Return 75.
Input: readings = [] Output: 0
subsetTotals is [0]. Keep [] and add those numbers. Return 0.
Input limits
- 0 <= input size <= 10.
- Answers fit in a signed 32-bit integer: -2,147,483,648 through 2,147,483,647.
- Input numbers have no decimal part. They range from -1,000,000 to 1,000,000. Follow any extra input rules in task 1.
Efficiency goal
Big-O describes how the work or memory needed grows as the input gets larger.
Efficiency goal: time O(n*2^n); extra memory, including the helper list, O(2^n). n is the input length. These labels describe how work and memory grow with the input.
Practice focus
Build subsetTotals first, then return exactly what the tasks ask for. Follow the order, keep repeated values when the rule says so, and check the empty case.