A calibration rig monitors n sensors that share one baseline reading array. All of the sensors are known to drift together: at any moment, sensor i's calibrated reading equals baseline[i] + d for one common integer offset d (which may be positive, negative, or zero) that is the same for every sensor at that moment. Each sensor also has its own admissible reading window [l_i, u_i]; a calibrated reading is valid for that sensor only if it falls inside its window, inclusive of both ends. Count how many integer offsets d make every sensor's calibrated reading fall inside its own window at the same time.
n, the number of sensors.n space-separated integers, the baseline readings baseline[0], ..., baseline[n-1].n lines contains two integers l_i and u_i: the admissible window for sensor i (0-indexed, in the same order as the baseline readings).Print a single integer: the number of valid integer offsets d (print 0 if none exist).
Example 1
Input
3 3 7 9 1 5 5 9 7 11
Expected
5
Explanation
Sensor 0 needs 1 <= 3+d <= 5, i.e. d in [-2, 2]. Sensor 1 needs 5 <= 7+d <= 9, i.e. d in [-2, 2]. Sensor 2 needs 7 <= 9+d <= 11, i.e. d in [-2, 2]. All three sensors allow exactly the same range [-2, 2], which contains 5 integers, so the answer is 5.
Example 2
Input
2 10 10 0 5 20 25
Expected
0
Explanation
Sensor 0 needs 0 <= 10+d <= 5, i.e. d in [-10, -5]. Sensor 1 needs 20 <= 10+d <= 25, i.e. d in [10, 15]. These two ranges never overlap, so there are 0 valid offsets.
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 →