All problems
0792MediumArrayHash TableGreedySorting

Recover the Base Readings

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2007Find Original Array From Doubled Array

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A calibration rig measures a batch of samples. For every sample the rig prints two rows on one sheet: the sample's base reading, and a second row holding exactly twice that base reading. A base reading of 0 therefore prints two rows of 0. The sheet then went through a shredder and was reassembled in some arbitrary order, giving the list sheet.

Work out the base readings the rig measured and return them in ascending order. If no set of base readings could have printed sheet — including the case where sheet holds an odd number of rows — return an empty list.

Examples

Example 1

Input
sheet = [6, 3, 1, 2]
Output
[1, 3]

Base reading 1 printed the rows 1 and 2, and base reading 3 printed the rows 3 and 6. Together those four rows are exactly the sheet, so the base readings are reported ascending.

Example 2

Input
sheet = [0, 0, 3, 6]
Output
[0, 3]

Base reading 0 printed two rows of 0, and base reading 3 printed the rows 3 and 6.

Example 3

Input
sheet = [3, 3, 6, 6, 12, 12]
Output
[]

There is no set of three base readings whose printed rows come to this sheet, so the empty list is returned.

Constraints

  • 1 <= sheet.length <= 10^5
  • 0 <= sheet[i] <= 10^5

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def recover_base_readings(sheet: list[int]) -> list[int]:
Java
public int[] recoverBaseReadings(int[] sheet)
September 7
Apply