A warehouse keeps two independent weighings of the same batch of n parcels: a before-list recorded on the old loading-dock scale, and an after-list recorded once the parcels were moved onto a replacement scale. The replacement scale was miscalibrated by a fixed integer offset x (x may be zero or negative), so every parcel's after-weight equals its before-weight plus x. The two lists may record the parcels in different orders, but once the offset is applied to every before-weight, the resulting values form exactly the same multiset as the after-list. Given both lists, determine the calibration offset x.
Print a single integer: the calibration offset x such that adding x to every value in the before-list produces exactly the multiset of the after-list.
Example 1
Input
3 2 6 4 9 7 5
Expected
3
Explanation
Adding 3 to each before-weight (2, 6, 4) gives (5, 9, 7), which as a multiset is exactly {5, 7, 9} -- the same as the after-list {9, 7, 5} -- so the offset is 3.
Example 2
Input
1 10 5
Expected
-5
Explanation
With a single parcel, the before-weight 10 became 5 on the replacement scale, so the offset is 5 - 10 = -5.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →