Find original array from encrypted array

QUESTION

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.

“TESTCASE_1”: “5\n6 8 9 2 1\n###—###SEPERATOR—###—\n0 -2 -3 4 5”, “TESTCASE_2”: “6\n1 2 3 4 5 6\n###—###SEPERATOR—###—\n3 2 1 0 -1 -2”, “TESTCASE_3”: “7\n1 23 56 4 8 45 32\n###—###SEPERATOR—###—\n27 5 -28 24 20 -17 -4”, “TESTCASE_4”: “4\n0 -1 -3 8\n###—###SEPERATOR—###—\n1 2 4 -7”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include<iostream>
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 i,arr_sum = 0;
    for ( 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 (i=0; i<n; i++)
        cout << (arr_sum - arr[i]) << " ";
}
 
// Driver program to test above
int main()
{
  int arr[30],n,i;
  scanf("%d",&n);
  for(i=0;i<n;i++)
    scanf("%d",&arr[i]);
  findAndPrintOriginalArray(arr, n);
    return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock