Selection proof: Peak ticket
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. - Find the largest number in
subsetTotals. Store it inlargest. - Complete
selectionProofPeakTicketso it returnslargest. Return it, do not print it.
Example
If readings is [2, 10, 7, 7, -2], then subsetTotals is [-2, 0, 2, 5, 7, 8, 9, 10, 12, 14, 15, 16, 17, 19, 22, 24, 26] and selectionProofPeakTicket returns 26.
Test data
TEST DATA 1: readings is [0, -11, 13, 13, 10].
TEST DATA 2: readings is [6, -6, 6, 6].
Examples
Input: readings = [0,-11,13,13,10] Output: 36
subsetTotals is [-11, -1, 0, 2, 10, 12, 13, 15, 23, 25, 26, 36]. The largest number is 36. Return 36.
Input: readings = [6,-6,6,6] Output: 18
subsetTotals is [-6, 0, 6, 12, 18]. The largest number is 18. Return 18.
Input: readings = [] Output: 0
subsetTotals is [0]. The largest number is 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.