Recover the Original Array
master · 1675 · lc hard +32 · verified · 41.3% accepted · 392 likes · top 22%
Description
Alice originally had a 0-indexed array arr of n positive integers. She chose a positive integer k and created two new arrays:
- lower[i] = arr[i] - k for every index i where 0 <= i < n
- higher[i] = arr[i] + k for every index i where 0 <= i < n
Alice lost all three arrays, but she remembers which integers appeared in lower and higher (though not which integer belonged to which array).
Given a combined array nums of 2n integers — exactly n from lower and n from higher — recover and return the original array arr. If multiple valid arrays exist, return any of them.
Note: Test cases guarantee at least one valid arr exists.
Example 1:
Example 2:
Example 3:
Code