Replace every element of the array by sum of all other elements - GeeksforGeeks

17 May, 2021
Given an array of size N, the task is to find the encrypted array. The encrypted array is obtained by replacing each element of the original array with the sum of the remaining array elements i.e. 
For every i, arr[i] = sumOfArrayElements – arr[i]
Examples:  
Input:  arr[] = {5, 1, 3, 2, 4} 
Output: 10 14 12 13 11
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[] = {6, 8, 32, 12, 14, 10, 25 }
Output: 101 99 75 95 93 97 82 
 
This problem is similar to Find original array from encrypted array (An array of sums of other elements).Approach:  
Find the sum of all elements of the array.Traverse the array and replace arr[i] with the sum-arr[i].
Find the sum of all elements of the array.
Traverse the array and replace arr[i] with the sum-arr[i].
 
C++
Java
Python 3
C#
PHP
Javascript
// C++ implementation to find encrypted array// from the original array#include <bits/stdc++.h>using namespace std; // Finds and prints the elements of the encrypted// arrayvoid findEncryptedArray(int arr[], int n){    // total sum of elements    // of original array    int sum = 0;    for (int i = 0; i < n; i++)        sum += arr[i];     // calculating and displaying    // elements of encrypted array    for (int i = 0; i < n; i++)        cout << (sum - arr[i]) << " ";} // Driver programint main(){    int arr[] = { 5, 1, 3, 2, 4 };    int N = sizeof(arr) / sizeof(arr[0]);    findEncryptedArray(arr, N);    return 0;}
// Java implementation to find encrypted// array from the original  array class GFG {    // Finds and prints the elements    // of the encrypted array    static void findEncryptedArray(int arr[], int n)    {        // total sum of elements        // of original array        int sum = 0;        for (int i = 0; i < n; i++)            sum += arr[i];         // calculating and displaying        // elements of encrypted array        for (int i = 0; i < n; i++)            System.out.print(sum - arr[i] + " ");    }     // Driver program    public static void main(String[] args)    {        int arr[] = { 5, 1, 3, 2, 4 };        int N = arr.length;        findEncryptedArray(arr, N);    }}
# Python 3 implementation# to find encrypted array# from the original array # Finds and prints the elements# of the encrypted arraydef findEncryptedArray(arr, n) :    sum = 0     # total sum of elements    # of original array    for i in range(n) :        sum += arr[i]     # calculating and displaying    # elements of encrypted array    for i in range(n) :        print(sum - arr[i], end = " ")         # Driver Codeif __name__ == "__main__" :     arr = [ 5, 1, 3, 2, 4]    N = len(arr)         # function calling    findEncryptedArray(arr, N) # This code is contributed by ANKITRAI1
// C# implementation to find// encrypted array from the// original arrayusing System; class GFG{// Finds and prints the elements// of the encrypted arraystatic void findEncryptedArray(int []arr,                               int n){    // total sum of elements    // of original array    int sum = 0;    for (int i = 0; i < n; i++)        sum += arr[i];     // calculating and displaying    // elements of encrypted array    for (int i = 0; i < n; i++)        Console.Write(sum - arr[i] + " ");} // Driver Codepublic static void Main(){    int []arr = { 5, 1, 3, 2, 4 };    int N = arr.Length;    findEncryptedArray(arr, N);}} // This code is contributed// by inder_verma.
<?php// PHP implementation to// find encrypted array// from the original array // Finds and prints the// elements of the encrypted// arrayfunction findEncryptedArray(&$arr, $n){    // total sum of elements    // of original array    $sum = 0;    for ($i = 0; $i < $n; $i++)        $sum += $arr[$i];     // calculating and displaying    // elements of encrypted array    for ($i = 0; $i < $n; $i++)        echo ($sum - $arr[$i]) . " ";} // Driver Code$arr = array(5, 1, 3, 2, 4 );$N = sizeof($arr);findEncryptedArray($arr, $N); // This code is contributed// by ChitraNayal?>
<script> // JavaScript implementation to find encrypted// array from the original  array         // Finds and prints the elements    // of the encrypted array    function findEncryptedArray(arr,n)    {        // total sum of elements        // of original array        let sum = 0;        for (let i = 0; i < n; i++)            sum += arr[i];           // calculating and displaying        // elements of encrypted array        for (let i = 0; i < n; i++)            document.write(sum - arr[i] + " ");    }         // Driver program    let arr=[5, 1, 3, 2, 4 ];    let N = arr.length;    findEncryptedArray(arr, N);       // This code is contributed by rag2127 </script>
10 14 12 13 11
 
Time complexity: O(n) 
inderDuMCA
ankthon
ukasp
rag2127
prefix-sum
suffix-sum
Arrays
prefix-sum
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Stack Data Structure (Introduction and Program)
Introduction to Arrays
Multidimensional Arrays in Java
Linear Search
Linked List vs Array
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Python | Using 2D arrays/lists the right way
Search an element in a sorted and rotated array