Ctrl K

Original Array From Two Shifts

Hard
There was an array base of n positive integers. Someone picked a positive integer k and built two arrays: one with every number decreased by k, the other with every number increased by k. All 2n numbers were then shuffled together into mixed, so it is unknown which number came from which array. Recover base. If several values of k fit, use the smallest one; for a fixed k, base is determined uniquely. The numbers may be returned in any order. At least one suitable k is guaranteed to exist.
Example 1
mixed=[9, 3, 13, 7] → [5, 11]

With k = 2, [5, 11] gives [3, 9] and [7, 13]. k = 3 also works ([6, 10]), but the smallest k is required, and k = 1 does not work.

Example 2
mixed=[4, 4, 10, 10] → [7, 7]

The only option is k = 3 with the original [7, 7]: pairing 4 with 4 would need k = 0, and k must be positive.

Example 3
mixed=[2, 100] → [51]
Constraints
  • len(mixed) = 2n, 1 ≤ n ≤ 1000
  • 1 ≤ mixed[i] ≤ 10⁹
  • A suitable base and k exist
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFlowerbed Beauty on a Budget HardTwins Within Two Swaps HardValues in Both Lists EasyShared Values With Repeats EasyA Subsequence of One Word Only Medium
loading Python…
Input
mixed=[9, 3, 13, 7]
Expected
[5, 11]

Run checks the first 3 tests in your browser. Submit checks all 78 tests on the server, hidden ones included.