Selection proof: Boss receipt
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. - Multiply each number in
subsetTotalsby its position, counting from 1: the first number by 1, the second by 2, and so on. - Add those products. Store the sum in
weightedTotal. - Complete
selectionProofBossReceiptso it returnsweightedTotal. Return it, do not print it.
Example
If readings is [0, 5, -1, 5, 9], then subsetTotals is [-1, 0, 4, 5, 8, 9, 10, 13, 14, 18, 19] and selectionProofBossReceipt returns 814.
Test data
TEST DATA 1: readings is [16, 19, 0, -11, 19].
TEST DATA 2: readings is [4, 4, -4, 4].
Examples
Input: readings = [16,19,0,-11,19] Output: 2430
subsetTotals is [-11, 0, 5, 8, 16, 19, 24, 27, 35, 38, 43, 54]. Multiply by positions that start at 1: 1 * (-11) + 2 * (0) + 3 * (5) + 4 * (8) + 5 * (16) + 6 * (19) + 7 * (24) + 8 * (27) + 9 * (35) + 10 * (38) + 11 * (43) + 12 * (54) = 2430. Return 2430.
Input: readings = [4,4,-4,4] Output: 100
subsetTotals is [-4, 0, 4, 8, 12]. Multiply by positions that start at 1: 1 * (-4) + 2 * (0) + 3 * (4) + 4 * (8) + 5 * (12) = 100. Return 100.
Input: readings = [] Output: 0
subsetTotals is [0]. Multiply by positions that start at 1: 1 * (0) = 0. 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.