# Construct an array from its pair-sum array

Given a pair-sum array and size of the original array (n), construct the original array.

A pair-sum array for an array is the array that contains sum of all pairs in ordered form. For example pair-sum array for arr[] = {6, 8, 3, 4} is {14, 9, 10, 11, 12, 7}.

In general, pair-sum array for arr[0..n-1] is {arr+arr, arr+arr, ……., arr+arr, arr+arr, ……., arr+arr, arr+arr, …., arr[n-2]+arr[n-1}.

“Given a pair-sum array, construct the original array.”

We strongly recommend to minimize your browser and try this yourself.

Let the given array be “pair[]” and let there be n elements in original array. If we take a look at few examples, we can observe that arr is half of pair + pair – pair[n-1]. Note that the value of pair + pair – pair[n-1] is (arr + arr) + (arr + arr) – (arr + arr).
Once we have evaluated arr, we can evaluate other elements by subtracting arr. For example arr can be evaluated by subtracting arr from pair, arr can be evaluated by subtracting arr from pair.

Following is the implementation of the above idea.

## C++

 `#include ` `using` `namespace` `std; ` ` `  `// Fills element in arr[] from its pair sum array pair[].  ` `// n is size of arr[] ` `void` `constructArr(``int` `arr[], ``int` `pair[], ``int` `n) ` `{ ` `    ``arr = (pair+pair-pair[n-1]) / 2; ` `    ``for` `(``int` `i=1; i

## Java

 `import` `java.io.*; ` ` `  `class` `PairSum { ` `     `  `    ``// Fills element in arr[] from its pair sum array pair[].  ` `    ``// n is size of arr[] ` `    ``static` `void` `constructArr(``int` `arr[], ``int` `pair[], ``int` `n) ` `    ``{ ` `        ``arr[``0``] = (pair[``0``]+pair[``1``]-pair[n-``1``]) / ``2``; ` `        ``for` `(``int` `i=``1``; i

## Python3

 `# Fills element in arr[] from its  ` `# pair sum array pair[].  ` `# n is size of arr[]  ` ` `  `def` `constructArr(arr,pair,n): ` `    ``arr [``0``] ``=` `(pair[``0``]``+``pair[``1``]``-``pair[n``-``1``])``/``/``2` `    ``for` `i ``in` `range``(``1``,n): ` `        ``arr[i] ``=` `pair[i``-``1``]``-``arr[``0``] ` ` `  `# Driver code ` `if` `__name__``=``=``'__main__'``: ` `    ``pair ``=` `[``15``, ``13``, ``11``, ``10``, ``12``, ``10``, ``9``, ``8``, ``7``, ``5``] ` `    ``n ``=``5` `    ``arr ``=` `[``0``]``*``n ` `    ``constructArr(arr,pair,n) ` `    ``for` `i ``in` `range``(n): ` `        ``print``(arr[i],end ``=``" "``) ` ` `  `# This code is contributed by  ` `# Shrikant13 `

## C#

 `// C# program to construct an ` `// array from its pair-sum array ` `using` `System; ` ` `  `class` `PairSum  ` `{ ` `    ``// Fills element in arr[] from its ` `    ``// pair sum array pair[].  ` `    ``// n is size of arr[] ` `    ``static` `void` `constructArr(``int` `[]arr, ``int` `[]pair, ` `                                             ``int` `n) ` `    ``{ ` `        ``arr = (pair + pair - pair[n - 1]) / 2; ` `        ``for` `(``int` `i = 1; i < n; i++) ` `            ``arr[i] = pair[i - 1] - arr; ` `    ``} ` `     `  `    ``// Driver program to test above function ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `[]pair = {15, 13, 11, 10, 12, ` `                         ``10, 9, 8, 7, 5}; ` `        ``int` `n = 5; ` `        ``int` `[]arr = ``new` `int``[n]; ` `        ``constructArr(arr, pair, n); ` `        ``for` `(``int` `i = 0; i < n; i++) ` `            ``Console.Write(arr[i] + ``" "``);      ` `    ``} ` `} ` ` `  `// This code is contributed by nitin mittal `

## PHP

 ` `

Output :

` 8 7 5 3 2 `

Time complexity of constructArr() is O(n) where n is number of elements in arr[].

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Article Tags :

4

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.