Given an array constructed from another array A by taking the sum of every distinct pair in it, decode it to get the original array A back.

If the original array is A[0], A[1], … , A[n-1], then the input array is

{
  (A[0] + A[1]), (A[0] + A[2]), … (A[0] + A[n-1]),
  (A[1] + A[2]), (A[1] + A[3]), … (A[1] + A[n-1]),
  …
  …
  (A[i] + A[i+1]), (A[i] + A[i+2]), … (A[i] + A[n-1]),
  …
  …
  (A[n-2] + A[n-1])
}

For example,

Input:  { 3, 4, 5, 5, 6, 7 }
Output: { 1, 2, 3, 4 }
 
 
Input:  { 3, 4, 5, 6, 5, 6, 7, 7, 8, 9 }
Output: { 1, 2, 3, 4, 5 }
 
 
Input:  { 3 }
Output: { 1, 2 } or { 2, 1 } or { 0, 3 } or { 3, 0 }
 
 
Input:  { 3, 4, 5 }
Output: { 1, 2, 3 }

Practice this problem

The idea is to calculate the first element of the original array by using the following relation:

A[0] = (inp[0] + inp[1] - inp[n-1]) / 2

As per problem constraints,

  inp[0] = A[0] + A[1]
  inp[1] = A[0] + A[2]
  inp[n-1] = A[1] + A[2]

Note that the above relation works only when the size of the input array m is more than 2. For m <= 2, we need to handle the code separately.

 
Once we know the first element, we can easily derive the remaining elements of the original array by using the relation A[i] = inp[i-1] - A[0].

 
Note that if m is the size of the given array and n is the size of the original array, then the relation between m and n is m = n×(n-1)/2, or n2-n-2×m = 0.

Solving the above equation, we get n = (sqrt(8×m+1)+1)/2. Refer to this link for proof of correctness of the above equation.

 
The algorithm can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Output:

1 2 3 4 5

Java


Download  Run Code

Output:

[1, 2, 3, 4, 5]

Python


Download  Run Code

Output:

[1, 2, 3, 4, 5]

The time complexity of the above solution is O(n) and requires O(n) extra space.