Selection proof: First qualifying slip
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. - Read
subsetTotalsfrom left to right. Find the first number that is at leastkand store that number, not its position, infirstMatch. - Complete
selectionProofFirstQualifyingSlipso it returnsfirstMatch. Return it, do not print it.
Example
If readings is [3, 3, 6, -5, 11] and k is 4, then subsetTotals is [-5, -2, 0, 1, 3, 4, 6, 7, 9, 11, 12, 14, 15, 17, 18, 20, 23] and selectionProofFirstQualifyingSlip returns 4.
Rules
- If no number is at least
k, return -1. k(a whole number from 1 to 8) is the threshold in task 2: compare each number with it.
Test data
TEST DATA 1: readings is [11, 11, 13, 0, -9] and k is 2.
TEST DATA 2: readings is [9, 9, 9, -6] and k is 8.
Examples
Input: readings = [11,11,13,0,-9] k = 2 Output: 2
subsetTotals is [-9, 0, 2, 4, 11, 13, 15, 22, 24, 26, 35]. Here k is 2. The first number at least 2 is 2. Return that value.
Input: readings = [9,9,9,-6] k = 8 Output: 9
subsetTotals is [-6, 0, 3, 9, 12, 18, 21, 27]. Here k is 8. The first number at least 8 is 9. Return that value.
Input: readings = [] k = 5 Output: -1
subsetTotals is [0]. Here k is 5. No number is at least 5, so return -1.
Input limits
- 0 <= input size <= 10.
- k is a whole number from 1 to 8. 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.