Member-only story
Leetcode 2122: Recover the Original Array
In this problem, one array was “mixed up” and we are trying to recover the original array. Any valid possibility is okay to return and we don’t need to return the entire set of valid answers.
Alice had a 0-indexed array
arr
consisting ofn
positive integers. She chose an arbitrary positive integerk
and created two new 0-indexed integer arrayslower
andhigher
in the following manner:lower[i] = arr[i] - k
, for every indexi
where0 <= i < n
, for every index
higher[i] = arr[i] + ki
where0 <= i < n
Unfortunately, Alice lost all three arrays. However, she remembers the integers that were present in the arrays
lower
andhigher
, but not the array each integer belonged to. Help Alice and recover the original array.
Given an arraynums
consisting of2n
integers, where exactlyn
of the integers were present inlower
and the remaining inhigher
, return the original arrayarr
. In case the answer is not unique, return any valid array.Note: The test cases are generated such that there exists at least one valid array
arr
.
We are given a few examples:
Unfortunately, these examples are too easy to solve and don’t offer a lot of insights on how to solve the problem. The way I see the problem, we need to “assign” low or high to signify the number was part of the original low or hi…