2
def recoverArray(self, nums):5
# All possible k are (nums[j] - nums[0]) // 2, otherwise there is no num that satisfies nums[0] + k = num - k.6
# For nums is sorted, so that any 2 elements (x, y) in nums[1:j] cannot satisfy x + k = y - k.7
# In other words, for any x in nums[1:j], it needs to find y from nums[j + 1:] to satisfy x + k = y - k, but8
# unfortunately if j > mid, then len(nums[j + 1:]) < mid <= len(nums[1:j]), nums[j + 1:] are not enough.9
# The conclusion is j <= mid.10
# If you think it’s not easy to understand why mid is enough, len(nums) can also work well11
# for j in range(1, len(nums)):12
for j in range(1, mid + 1): # O(N)14
nums[j] - nums[0] > 0 and (nums[j] - nums[0]) % 2 == 015
): # Note the problem described k is positive.17
(nums[j] - nums[0]) // 2,18
collections.Counter(nums),21
# For each number in lower, we try to find the corresponding number from higher list.22
# Because nums is sorted, current n is always the current lowest num which can only come from lower23
# list, so we search the corresponding number of n which equals to n + 2 * k in the left24
# if it can not be found, change another k and continue to try.29
): # check if n + 2 * k available as corresponding number in higher list of n32
): # removed by previous num as its corresponding number in higher list35
counter[n + 2 * k] == 036
): # not found corresponding number in higher list39
counter[n] -= 1 # remove n42
] -= 1 # remove the corresponding number in higher list