Sorting press: Total receipt
Stuck or curious how this works? Visualize Binary Search ↗
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
sorted. Sortreadingsfrom smallest to largest using merge sort. To use merge sort, split the list into smaller parts, sort those parts, then join the sorted parts in order. Keep all repeated numbers.sortedis the sorted list. - Add up every number in
sorted. Store the sum intotal. - Complete
sortingPressTotalReceiptso it returnstotal. Return it, do not print it.
Example
If readings is [1, 3, -4, 7, 7], then sorted is [-4, 1, 3, 7, 7] and sortingPressTotalReceipt returns 14.
Rules
- If
sortedis empty,totalis 0.
Test data
TEST DATA 1: readings is [20, 18, 0, -11, 18].
TEST DATA 2: readings is [4, 4, 4, -9].
Examples
Input: readings = [20,18,0,-11,18] Output: 45
sorted is [-11, 0, 18, 18, 20]. Add its numbers to get 45. Return 45.
Input: readings = [4,4,4,-9] Output: 3
sorted is [-9, 4, 4, 4]. Add its numbers to get 3. Return 3.
Input: readings = [] Output: 0
sorted is []. Add its numbers to get 0. Return 0.
Input limits
- 0 <= input size <= 64.
- 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 log n); extra memory, including the helper list, O(n). n is the input length. These labels describe how work and memory grow with the input.
Practice focus
Build sorted 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.