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 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
Maximize Array sum by adding multiple of another Array element in given ranges Given two arrays X[] and Y[] of length N along with Q queries each of type [L, R] that denotes the subarray of X[] from L to R. The task is to find the maximum sum that can be obtained by applying the following operation for each query: Choose an element from Y[]. Add multiples with alternate +ve an
15+ min read
Modify array to another given array by replacing array elements with the sum of the array | Set-2 Given an Array input[] consisting only of 1s initially and an array target[] of size N, the task is to check if the array input[] can be converted to target[] by replacing input[i] with the sum of array elements in each step. Examples: Input: input[] = { 1, 1, 1 }, target[] = { 9, 3, 5 } Output: YES
10 min read