# 1. Introduction

This blog post addresses an intriguing problem of decoding an integer array to its original form. Given an array that is constructed by summing every distinct pair from another array, the task is to reconstruct the original array. This problem is an excellent exercise in mathematical reasoning and array manipulation.

## Problem

We are given an integer array X, which is formed by taking the sum of every distinct pair from another array Y. The challenge is to decode X to retrieve the original array Y.

# 2. Solution Steps

1. Understand that the smallest two elements in X are the sums of the first two elements of Y with the rest.

2. Calculate the difference between these two elements to find the smallest element of Y.

3. Subtract this smallest element from each element in X to form a new array.

4. Recursively solve the problem for this new array.

5. Continue this process until the original array Y is reconstructed.

# 3. Code Program

``````#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> decodeArray(const vector<int>& X) {
if (X.size() == 1) return {X[0] / 2, X[0] / 2};

int smallestY = (X[0] - X[1]) / 2;
vector<int> smallerX;
for (int x : X) {
smallerX.push_back(x - smallestY);
}

vector<int> Y = decodeArray(vector<int>(smallerX.begin() + 1, smallerX.end()));
Y.insert(Y.begin(), smallestY);
return Y;
}

int main() {
vector<int> X = {3, 4, 5, 5, 6, 7};
vector<int> Y = decodeArray(X);

// Output the original array Y
cout << "Original Array: ";
for (int y : Y) {
cout << y << " ";
}
cout << endl;

return 0;
}

``````

### Output:

```Original Array: 1 2 3 4
```

### Explanation:

The function decodeArray reconstructs the original array Y from the given sum array X. It starts by identifying the smallest element of Y using the difference between the smallest two elements in X. It then reduces the problem to a smaller instance by removing the effects of this smallest element and recursively applying the same logic. For example, for the input array [3, 4, 5, 5, 6, 7], the function correctly reconstructs the original array [1, 2, 3, 4].