Related Articles

# Find original array from encrypted array (An array of sums of other elements)

• Difficulty Level : Easy
• Last Updated : 20 May, 2021

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.
Examples :

```Input :  arr[] = {10, 14, 12, 13, 11}
Output : {5, 1, 3, 2, 4}
Original array {5, 1, 3, 2, 4}
Encrypted array is obtained as:
= {1+3+2+4, 5+3+2+4, 5+1+2+4, 5+1+3+4, 5+1+3+2}
= {10, 14, 12, 13, 11}
Each element of original array is replaced by the
sum of the remaining array elements.

Input : arr[] = {95, 107, 103, 88, 110, 87}
Output : {23, 11, 15, 30, 8, 31}```

Approach is purely based on arithmetic observations which are illustrated below:

```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 = (b+c+d), arr = (a+c+d),
arr = (a+b+d), arr = (a+b+c)
sum =  arr + arr + arr + arr
= (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+arr+....+arr[n-1]) / (n-1)

Then, elements of ori[] are calculated as:
ori = sum - arr
ori = sum - arr
.
.
ori[n-1] = sum - arr[n-1]                      ```

Below is the implementation of above steps.

## C++

 `// C++ implementation to find original array``// from the encrypted array``#include ``using` `namespace` `std;` `// Finds and prints the elements of the original``// array``void` `findAndPrintOriginalArray(``int` `arr[], ``int` `n)``{``    ``// total sum of elements``    ``// of encrypted array``    ``int` `arr_sum = 0;``    ``for` `(``int` `i=0; i

## Python 3

 `# Python 3 implementation to find``# original array from the encrypted``# array` `# Finds and prints the elements of``# the original array``def` `findAndPrintOriginalArray(arr, n):` `    ``# total sum of elements``    ``# of encrypted array``    ``arr_sum ``=` `0``    ``for` `i ``in` `range``(``0``, n):``        ``arr_sum ``+``=` `arr[i]` `    ``# total sum of elements``    ``# of original array``    ``arr_sum ``=` `int``(arr_sum ``/` `(n ``-` `1``))` `    ``# calculating and displaying``    ``# elements of original array``    ``for` `i ``in` `range``(``0``, n):``        ``print``((arr_sum ``-` `arr[i]),``                       ``end ``=` `" "``)` `# Driver program to test above``arr ``=` `[``10``, ``14``, ``12``, ``13``, ``11``]``n ``=` `len``(arr)``findAndPrintOriginalArray(arr, n)` `# This code is contributed By Smitha`

## C#

 `// C# program to find original``// array from the encrypted array``using` `System;` `class` `GFG {``    ` `    ``// Finds and prints the elements``    ``// of the original array``    ``static` `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++)``        ``Console.Write(arr_sum - arr[i] + ``" "``);``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main (String[] args)``    ``{``        ``int` `[]arr = {10, 14, 12, 13, 11};``        ``int` `n =arr.Length;``        ``findAndPrintOriginalArray(arr, n);``    ``}``}` `// This code is contributed by parashar...`

## PHP

 ``

## Javascript

 ``

Output :

`5 1 3 2 4`

Time complexity: O(N)

Auxiliary Space: O(1)
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.