Find original array from encrypted array (An array of sums of other elements)
Last Updated :
11 Jul, 2022
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[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]
Below is the implementation of above steps.
C++
// C++ implementation to find original array
// from the encrypted array
#include <bits/stdc++.h>
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<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]) << " ";
}
// Driver program to test above
int main()
{
int arr[] = {10, 14, 12, 13, 11};
int n = sizeof(arr) / sizeof(arr[0]);
findAndPrintOriginalArray(arr, n);
return 0;
}
Java
import java.util.*;
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++) {
System.out.print(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 rj13to.
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
<?php
// PHP implementation to find
// original array from the
// encrypted array
// Finds and prints the elements
// of the original array
function findAndPrintOriginalArray($arr, $n)
{
// total sum of elements
// of encrypted array
$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++)
echo $arr_sum - $arr[$i] , " ";
}
// Driver Code
$arr = array(10, 14, 12, 13, 11);
$n = count($arr);
findAndPrintOriginalArray($arr, $n);
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// Javascript program to find original
// array from the encrypted array
// Finds and prints the elements
// of the original array
function findAndPrintOriginalArray(arr, n)
{
// total sum of elements
// of encrypted array
let arr_sum = 0;
for (let i = 0; i < n; i++)
arr_sum += arr[i];
// total sum of elements
// of original array
arr_sum = parseInt(arr_sum / (n - 1), 10);
// calculating and displaying
// elements of original array
for (let i = 0; i < n; i++)
document.write(arr_sum - arr[i] + " ");
}
let arr = [10, 14, 12, 13, 11];
let n =arr.length;
findAndPrintOriginalArray(arr, n);
// This code is contributed by rameshtravel07.
</script>
Time complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Find original Array from given Array where each element is sum of prefix and postfix sum
Given an array arr[] of length N, where arr is derived from an array nums[] which is lost. Array arr[] is derived as: arr[i] = (nums[0] + nums[1] + ... + nums[i]) + (nums[i] + nums[i+1] + ... + nums[N-1]). The task is to find nums[] array of length N. Examples: Input: N = 4, arr[] = {9, 10, 11, 10}O
10 min read
Find an element in array such that sum of left array is equal to sum of right array
Given, an array of size n. Find an element that divides the array into two sub-arrays with equal sums. Examples: Input: 1 4 2 5 0Output: 2Explanation: If 2 is the partition, subarrays are : [1, 4] and [5] Input: 2 3 4 1 4 5Output: 1Explanation: If 1 is the partition, Subarrays are : [2, 3, 4] and [4
15+ min read
Find the original Array from given array where ith element is the average of first i elements
Given an array arr[] of length N, the task is to find the original array such that every ith element in the given array(arr[i]) is the average value of the first i elements of the original array. Examples: Input: arr = {4 3 3 3} Output: 4 2 3 3Explanation: (4) / 1 = 1, (4+2) / 2 = 3, (4+2+3) / 3 = 3
5 min read
Sum of Bitwise OR of every array element paired with all other array elements
Given an array arr[] consisting of non-negative integers, the task for each array element arr[i] is to print the sum of Bitwise OR of all pairs (arr[i], arr[j]) ( 0 ⤠j ⤠N ). Examples: Input: arr[] = {1, 2, 3, 4}Output: 12 14 16 22Explanation:For i = 0 the required sum will be (1 | 1) + (1 | 2) + (
11 min read
For all Array elements find Product of Sum of all smaller and Sum of all greater elements
Given an array arr[] of integers of length N, the task is to find the product of the sum of all the numbers larger than that number with the sum of all the numbers less than that number for each number in the array. Examples: Input: arr[] = {8, 4, 9, 3}, N = 4Output:- 63, 51, 0, 0Explanation:For fir
15 min read
Sum of Bitwise OR of each array element of an array with all elements of another array
Given two arrays arr1[] of size M and arr2[] of size N, the task is to find the sum of bitwise OR of each element of arr1[] with every element of the array arr2[]. Examples: Input: arr1[] = {1, 2, 3}, arr2[] = {1, 2, 3}, M = 3, N = 3Output: 7 8 9Explanation: For arr[0]: Sum = arr1[0]|arr2[0] + arr1[
11 min read
Find all indices of Array having value same as average of other elements
Given an array arr[] of N integers, the task is to find all indices in the array, such that for each index i the arithmetic mean of all elements except arr[i] is equal to the value of the element at that index. Examples : Input: N = 5, arr[] = {1, 2, 3, 4, 5}Output : {2}Explanation : Upon removing a
6 min read
Find elements in given Array that are a factor of sum of remaining elements
Given an array A[] of size N, the task is to find the elements in the array which are factors of the sum of the remaining element. So just select an element from an array and take the sum of the remaining elements and check whether the sum is perfectly divisible by the selected element or not. If it
11 min read
Find if array has an element whose value is half of array sum
Given a sorted array (with unique entries), we have to find whether there exists an element(say X) that is exactly half the sum of all the elements of the array including X. Examples: Input : A = {1, 2, 3} Output : YES Sum of all the elements is 6 = 3*2; Input : A = {2, 4} Output : NO Sum of all the
10 min read
Find array elements equal to sum of any subarray of at least size 2
Given an array arr[], the task is to find the elements from the array which are equal to the sum of any sub-array of size greater than 1.Examples: Input: arr[] = {1, 2, 3, 4, 5, 6} Output: 3, 5, 6 Explanation: The elements 3, 5, 6 are equal to sum of subarrays {1, 2},{2, 3} and {1, 2, 3} respectivel
6 min read