Let be the smallest element of the original array (which needs to be restored). After adding to each element of the array, the smallest element of the resulting array becomes .
To restore the original values, find the smallest element min in the input array. Since min = 2x, compute by dividing by : add = min / 2.
The value was added to all elements of the original array. Therefore, to restore the original values, subtract add from each element of the input array.
Example
In the given example, the smallest element is min = -4. Divide it by : add = min / 2 = -2. Now subtract from each element of the input array.
Algorithm implementation
Declare an array.
int m[101];Read the input array. Find its smallest element .
scanf("%d", &n);
min = 100;
for (i = 0; i < n; i++)
{
scanf("%d", &m[i]);
if (m[i] < min) min = m[i];
}Divide the smallest element by — this value was added to all elements of the original array.
add = min / 2;Subtract the value from each element of the input array. Simultaneously, print the restored (original) array.
for (i = 0; i < n; i++)
{
m[i] -= add;
printf("%d ", m[i]);
}
printf("\n");Python implementation
Read the input data.
n = int(input())
lst = list(map(int,input().split()))Find the value , which was added to all elements of the original list.
add = min(lst) // 2Subtract the value from each element of the list .
res = [x - add for x in lst]Print the restored (original) array.
print (*res)