Recover the Original Array

Alice had a 0-indexed array arr consisting of n positive integers. She chose an arbitrary positive integer k and created two new 0-indexed integer arrays lower and higher in the following manner:

  • 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

Unfortunately, Alice lost all three arrays. However, she remembers the integers that were present in the arrays lower and higher, but not the array each integer belonged to. Help Alice and recover the original array.

Given an array nums consisting of 2n integers, where exactly n of the integers were present in lower and the remaining in higher, return the original array arr. 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.

Example 1
Inputnums = [2,10,6,4,8,12]
Output[3,7,11]
If arr = [3, 7, 11] and k = 1, then lower = [2, 6, 10] and higher = [4, 8, 12], which combine to a permutation of nums; another valid answer is [5, 7, 9] with k = 3.
Example 2
Inputnums = [1,1,3,3]
Output[2,2]
If arr = [2, 2] and k = 1, then lower = [1, 1] and higher = [3, 3], while arr = [1, 3] would require invalid k = 0.

Constraints

  • 2 * n == nums.length
  • 1 <= n <= 1000
  • 1 <= nums[i] <= 10^9
  • The test cases are generated such that there exists at least one valid array arr.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate