Find original array from encrypted array (An array of sums of other elements) - GeeksforGeeks
Find original array from a given encrypted array of size n. Encrypted array is obtained by replacing each element of the original array by the sum of the remaining array elements.
Read full article from Find original array from encrypted array (An array of sums of other elements) - GeeksforGeeks
Find original array from a given encrypted array of size n. Encrypted array is obtained by replacing each element of the original array by the sum of the remaining array elements.
Let n = 4, and the original array be ori[] = {a, b, c, d} encrypted array is given as: arr[] = {b+c+d, a+c+d, a+b+d, a+b+c} Elements of encrypted array are : arr[0] = (b+c+d), arr[1] = (a+c+d), arr[2] = (a+b+d), arr[3] = (a+b+c) add up all the elements sum = arr[0] + arr[1] + arr[2] + arr[3] = (b+c+d) + (a+c+d) + (a+b+d) + (a+b+c) = 3(a+b+c+d) Sum of elements of ori[] = sum / n-1 = sum/3 = (a+b+c+d) Thus, for a given encrypted array arr[] of size n, the sum of the elements of the original array ori[] can be calculated as: sum = (arr[0]+arr[1]+....+arr[n-1]) / (n-1) Then, elements of ori[] are calculated as: ori[0] = sum - arr[0] ori[1] = sum - arr[1] . . ori[n-1] = sum - arr[n-1]
void
findAndPrintOriginalArray(
int
arr[],
int
n)
{
// total sum of elements
// of encrypted array
int
arr_sum = 0;
for
(
int
i=0; i<n; i++)
arr_sum += arr[i];
// total sum of elements
// of original array
arr_sum = arr_sum/(n-1);
// calculating and displaying
// elements of original array
for
(
int
i=0; i<n; i++)
cout << (arr_sum - arr[i]) <<
" "
;
}
Read full article from Find original array from encrypted array (An array of sums of other elements) - GeeksforGeeks