Construct an array from its pairsum array (original) (raw)

Last Updated : 22 Jul, 2025

Given a pair-sum array arr[], where each element represents the sum of a unique pair of elements from an unknown original array res[].
The pairs are considered in a specific order:

**Notes:

**Examples

**Input: arr[] = [4, 5, 3]
**Output: [3, 1, 2]
**Explanation: For [3, 1, 2], pairwise sums are (3 + 1), (3 + 2) and (1 + 2)

**Input: arr[] = [3]
**Output: [1, 2]
**Explanation: There may be multiple valid original arrays that produce the same pair-sum array, such as [1, 2] and [2, 1]. Both are considered correct since they yield the same set of pairwise sums.

Try It Yourselfredirect icon

[Approach] Pair-Sum Decomposition

The approach begins by determining the size n of the original array using the formula for the number of unique pairs: **n*(n-1)/2 = length of arr
Solving this gives the size of the original array.

Once n is known, we focus on the first three elements of the pair-sum array. These represent the sums:

By adding the first two and subtracting the third, we can isolate res[0]: **res[0] = (arr[0] + arr[1] - arr[2]) / 2.

After finding res[0], the rest of the values in res can be easily calculated. Since the next n - 1 values in arr represent res[0] + res[1], res[0] + res[2], ..., res[0] + res[n-1], we can subtract res[0] from each to find res[1] through res[n-1].

So, for each i from 1 to n - 1: **res[i] = arr[i-1] - res[0].

**Illustration:

23

C++ `

#include #include #include using namespace std;

vector constructArr(vector& arr) {

if(arr.size() == 1) 
    return {1, arr[0]-1} ;

// size of the result array
int n = (1 + sqrt(1 + 8 * arr.size())) / 2;

// find the result array
vector<int> res(n);
res[0] = (arr[0] + arr[1] - arr[n - 1]) / 2;
for (int i = 1; i < n; i++)
    res[i] = arr[i - 1] - res[0];
    
return res;

}

int main() { vector arr = {4, 5, 3}; vector res = constructArr(arr); for (int x : res) cout << x << " "; return 0; }

Java

import java.util.ArrayList; import java.util.List;

class GfG {

public static ArrayList<Integer> constructArr(int[] arr){
    
    // only one pair-sum ⇒ original array has two numbers
    if (arr.length == 1) {
        return new ArrayList<>(List.of(1, arr[0] - 1));
    }

    // compute n from m = n·(n−1)/2  
    // n = (1 + √(1 + 8m)) / 2
    int n = (int) ((1 + Math.sqrt(1 + 8 * arr.length)) / 2);

    int[] res = new int[n];
    
    // res[0] is obtained from the first three relevant sums
    res[0] = (arr[0] + arr[1] - arr[n - 1]) / 2;

    // remaining elements: subtract res[0] from 
    // successive pair sums
    for (int i = 1; i < n; i++) {
        res[i] = arr[i - 1] - res[0];
    }

    ArrayList<Integer> ans = new ArrayList<>(n);
    for (int x : res) ans.add(x);
    return ans;
}

public static void main(String[] args) {

    int[] arr = {4, 5, 3};
    ArrayList<Integer> res = constructArr(arr);

    for (int x : res) System.out.print(x + " ");
}

}

Python

import math

def constructArr(arr):

# when the pair-sum array has only one element,
# the original array must have exactly two numbers.
if len(arr) == 1:
    return [1, arr[0] - 1]

# size of the original array 
n = int((1 + math.isqrt(1 + 8 * len(arr))) / 2)

# reconstruct the original array
res = [0] * n
res[0] = (arr[0] + arr[1] - arr[n - 1]) // 2
for i in range(1, n):
    res[i] = arr[i - 1] - res[0]

return res

if name == "main": arr = [4, 5, 3] res = constructArr(arr) print(' '.join(map(str, res)))

C#

using System; using System.Collections.Generic;

class GfG{

static List<int> constructArr(int[] arr){
    
    // only one pair-sum ⇒ original array has two numbers
    if (arr.Length == 1)
        return new List<int> { 1, arr[0] - 1 };

    // compute n from m = n·(n−1)/2  ⇒  n = (1 + √(1 + 8m)) / 2
    int n = (int)((1 + Math.Sqrt(1 + 8 * arr.Length)) / 2);

    int[] res = new int[n];
    
    // res[0] is obtained from the first three relevant sums
    res[0] = (arr[0] + arr[1] - arr[n - 1]) / 2;

    // remaining elements: subtract res[0] from successive pair sums
    for (int i = 1; i < n; i++)
        res[i] = arr[i - 1] - res[0];

    return new List<int>(res);
}

static void Main(){
    
    int[] arr = { 4, 5, 3 };
    List<int> res = constructArr(arr);
    foreach (int x in res) Console.Write(x + " ");
}

}

JavaScript

function constructArr(arr) {

// only one pair-sum 
// the original array has exactly two numbers
if (arr.length === 1) {
    return [1, arr[0] - 1];
}

// size of the original array
const n = Math.floor((1 + Math.sqrt(1 + 8 * arr.length)) / 2);

// reconstruct the original array
const res = new Array(n);
res[0] = (arr[0] + arr[1] - arr[n - 1]) / 2;
for (let i = 1; i < n; i++) {
    res[i] = arr[i - 1] - res[0];
}

return res;

}

// Driver code const arr = [4, 5, 3]; const res = constructArr(arr); console.log(res.join(' '));

`

**Time Complexity: O(n), where n is the size of the original array, since we compute each element of res[] in a single pass.
**Auxiliary Space: O(n), for storing the output array res[], no extra space is used beyond that.