# Sum of the updated array after performing the given operation

Given an array arr[] of N elements, the task is to update all the array elements such that an element arr[i] is updated as arr[i] = arr[i] – X where X = arr[i + 1] + arr[i + 2] + … + arr[N – 1] and finally print the sum of the updated array.
Examples:

Input: arr[] = {40, 25, 12, 10}
Output:
The updated array will be {-7, 3, 2, 10}.
-7 + 3 + 2 + 10 = 8

Input: arr[] = {50, 30, 10, 2, 0}
Output: 36

Approach: A simple solution is for every possible value of i, update arr[i] = arr[i] – sum(arr[i+1…N-1]).

## C++

 `// C++ implementation of the approach` `#include ` `using` `namespace` `std;`   `// Utility function to return` `// the sum of the array` `int` `sumArr(``int` `arr[], ``int` `n)` `{` `    ``int` `sum = 0;` `    ``for` `(``int` `i = 0; i < n; i++)` `        ``sum += arr[i];` `    ``return` `sum;` `}`   `// Function to return the sum` `// of the modified array` `int` `sumModArr(``int` `arr[], ``int` `n)` `{`   `    ``for` `(``int` `i = 0; i < n - 1; i++) {`   `        ``// Find the sum of the subarray` `        ``// arr[i+1...n-1]` `        ``int` `subSum = 0;` `        ``for` `(``int` `j = i + 1; j < n; j++) {` `            ``subSum += arr[j];` `        ``}`   `        ``// Subtract the subarray sum` `        ``arr[i] -= subSum;` `    ``}`   `    ``// Return the sum of` `    ``// the modified array` `    ``return` `sumArr(arr, n);` `}`   `// Driver code` `int` `main()` `{` `    ``int` `arr[] = { 40, 25, 12, 10 };` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]);`   `    ``cout << sumModArr(arr, n);`   `    ``return` `0;` `}`

## Java

 `// Java implementation of the approach` `import` `java.util.*;`   `class` `GFG` `{`   `// Utility function to return` `// the sum of the array` `static` `int` `sumArr(``int` `arr[], ``int` `n)` `{` `    ``int` `sum = ``0``;` `    ``for` `(``int` `i = ``0``; i < n; i++)` `        ``sum += arr[i];` `    ``return` `sum;` `}`   `// Function to return the sum` `// of the modified array` `static` `int` `sumModArr(``int` `arr[], ``int` `n)` `{` `    ``for` `(``int` `i = ``0``; i < n - ``1``; i++)` `    ``{`   `        ``// Find the sum of the subarray` `        ``// arr[i+1...n-1]` `        ``int` `subSum = ``0``;` `        ``for` `(``int` `j = i + ``1``; j < n; j++)` `        ``{` `            ``subSum += arr[j];` `        ``}`   `        ``// Subtract the subarray sum` `        ``arr[i] -= subSum;` `    ``}`   `    ``// Return the sum of` `    ``// the modified array` `    ``return` `sumArr(arr, n);` `}`   `// Driver code` `public` `static` `void` `main(String []args)` `{` `    ``int` `arr[] = { ``40``, ``25``, ``12``, ``10` `};` `    ``int` `n = arr.length;`   `    ``System.out.println(sumModArr(arr, n));` `}` `}`   `// This code is contributed by Rajput-Ji`

## Python3

 `# Python3 implementation of the approach`   `# Utility function to return` `# the sum of the array` `def` `sumArr(arr, n):` `    ``sum` `=` `0` `    ``for` `i ``in` `range``(n):` `        ``sum` `+``=` `arr[i]` `    ``return` `sum`   `# Function to return the sum` `# of the modified array` `def` `sumModArr(arr, n):`   `    ``for` `i ``in` `range``(n ``-` `1``):`   `        ``# Find the sum of the subarray` `        ``# arr[i+1...n-1]` `        ``subSum ``=` `0` `        ``for` `j ``in` `range``(i ``+` `1``, n):` `            ``subSum ``+``=` `arr[j]` `            `  `        ``# Subtract the subarray sum` `        ``arr[i] ``-``=` `subSum`   `    ``# Return the sum of` `    ``# the modified array` `    ``return` `sumArr(arr, n)`   `# Driver code` `arr ``=` `[``40``, ``25``, ``12``, ``10``]` `n ``=` `len``(arr)`   `print``(sumModArr(arr, n))`   `# This code is contributed by Mohit Kumar`

## C#

 `// C# implementation of the approach` `using` `System;`   `class` `GFG` `{`   `    ``// Utility function to return` `    ``// the sum of the array` `    ``static` `int` `sumArr(``int` `[]arr, ``int` `n)` `    ``{` `        ``int` `sum = 0;` `        ``for` `(``int` `i = 0; i < n; i++)` `            ``sum += arr[i];` `        ``return` `sum;` `    ``}` `    `  `    ``// Function to return the sum` `    ``// of the modified array` `    ``static` `int` `sumModArr(``int` `[]arr, ``int` `n)` `    ``{` `        ``for` `(``int` `i = 0; i < n - 1; i++)` `        ``{` `    `  `            ``// Find the sum of the subarray` `            ``// arr[i+1...n-1]` `            ``int` `subSum = 0;` `            ``for` `(``int` `j = i + 1; j < n; j++)` `            ``{` `                ``subSum += arr[j];` `            ``}` `    `  `            ``// Subtract the subarray sum` `            ``arr[i] -= subSum;` `        ``}` `    `  `        ``// Return the sum of` `        ``// the modified array` `        ``return` `sumArr(arr, n);` `    ``}` `    `  `    ``// Driver code` `    ``public` `static` `void` `Main()` `    ``{` `        ``int` `[]arr = { 40, 25, 12, 10 };` `        ``int` `n = arr.Length;` `    `  `        ``Console.WriteLine(sumModArr(arr, n));` `    ``}` `}`   `// This code is contributed by AnkitRai01`

## Javascript

 ``

Output:

`8`

Time Complexity: O(N2)
Auxiliary Space: O(1)

Efficient approach: An efficient solution is to traverse the array from the end so that the sum of the subarray till now i.e. sum(arr[i+1…n-1]) can be used to calculate the sum of the current subarray arr[i…n-1] i.e. sum(arr[i…n-1]) = arr[i] + sum(arr[i+1…n-1]).
Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach` `#include ` `using` `namespace` `std;`   `// Utility function to return` `// the sum of the array` `int` `sumArr(``int` `arr[], ``int` `n)` `{` `    ``int` `sum = 0;` `    ``for` `(``int` `i = 0; i < n; i++)` `        ``sum += arr[i];` `    ``return` `sum;` `}`   `// Function to return the sum` `// of the modified array` `int` `sumModArr(``int` `arr[], ``int` `n)` `{`   `    ``int` `subSum = arr[n - 1];` `    ``for` `(``int` `i = n - 2; i >= 0; i--) {`   `        ``int` `curr = arr[i];`   `        ``// Subtract the subarray sum` `        ``arr[i] -= subSum;`   `        ``// Sum of subarray arr[i...n-1]` `        ``subSum += curr;` `    ``}`   `    ``// Return the sum of` `    ``// the modified array` `    ``return` `sumArr(arr, n);` `}`   `// Driver code` `int` `main()` `{` `    ``int` `arr[] = { 40, 25, 12, 10 };` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]);`   `    ``cout << sumModArr(arr, n);`   `    ``return` `0;` `}`

## Java

 `// Java implementation of the approach` `class` `GFG ` `{` `    `  `    ``// Utility function to return ` `    ``// the sum of the array ` `    ``static` `int` `sumArr(``int` `arr[], ``int` `n) ` `    ``{ ` `        ``int` `sum = ``0``; ` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `            ``sum += arr[i]; ` `        ``return` `sum; ` `    ``} ` `    `  `    ``// Function to return the sum ` `    ``// of the modified array ` `    ``static` `int` `sumModArr(``int` `arr[], ``int` `n) ` `    ``{ ` `        ``int` `subSum = arr[n - ``1``]; ` `        ``for` `(``int` `i = n - ``2``; i >= ``0``; i--)` `        ``{ ` `            ``int` `curr = arr[i]; ` `    `  `            ``// Subtract the subarray sum ` `            ``arr[i] -= subSum; ` `    `  `            ``// Sum of subarray arr[i...n-1] ` `            ``subSum += curr; ` `        ``} ` `    `  `        ``// Return the sum of ` `        ``// the modified array ` `        ``return` `sumArr(arr, n); ` `    ``} ` `    `  `    ``// Driver code ` `    ``public` `static` `void` `main (String[] args) ` `    ``{ ` `        ``int` `[]arr = { ``40``, ``25``, ``12``, ``10` `}; ` `        ``int` `n = arr.length; ` `    `  `        ``System.out.println(sumModArr(arr, n)); ` `    ``} ` `}`   `// This code is contributed by kanugargng`

## Python3

 `# Python3 implementation of the approach`   `# Function to return the sum` `# of the modified array` `def` `sumModArr(arr, n):`   `    ``subSum ``=` `arr[n ``-` `1``];` `    ``for` `i ``in` `range``(n ``-` `2``, ``-``1``, ``-``1``):`   `        ``curr ``=` `arr[i];`   `        ``# Subtract the subarray sum` `        ``arr[i] ``-``=` `subSum;`   `        ``# Sum of subarray arr[i...n-1]` `        ``subSum ``+``=` `curr;`   `    ``# Return the sum of` `    ``# the modified array` `    ``return` `sum``(arr)`   `# Driver code` `if` `__name__ ``=``=` `"__main__"``:` `  ``arr ``=` `[``40``, ``25``, ``12``, ``10` `];` `  ``n ``=` `len``(arr);`   `  ``print``(sumModArr(arr, n));`   `# This code is contributed by Shushant Kumar`

## C#

 `// C# implementation of the approach` `using` `System;` `    `  `class` `GFG ` `{` `    `  `    ``// Utility function to return ` `    ``// the sum of the array ` `    ``static` `int` `sumArr(``int` `[]arr, ``int` `n) ` `    ``{ ` `        ``int` `sum = 0; ` `        ``for` `(``int` `i = 0; i < n; i++) ` `            ``sum += arr[i]; ` `        ``return` `sum; ` `    ``} ` `    `  `    ``// Function to return the sum ` `    ``// of the modified array ` `    ``static` `int` `sumModArr(``int` `[]arr, ``int` `n) ` `    ``{ ` `        ``int` `subSum = arr[n - 1]; ` `        ``for` `(``int` `i = n - 2; i >= 0; i--)` `        ``{ ` `            ``int` `curr = arr[i]; ` `    `  `            ``// Subtract the subarray sum ` `            ``arr[i] -= subSum; ` `    `  `            ``// Sum of subarray arr[i...n-1] ` `            ``subSum += curr; ` `        ``} ` `    `  `        ``// Return the sum of ` `        ``// the modified array ` `        ``return` `sumArr(arr, n); ` `    ``} ` `    `  `    ``// Driver code ` `    ``public` `static` `void` `Main (String[] args) ` `    ``{ ` `        ``int` `[]arr = { 40, 25, 12, 10 }; ` `        ``int` `n = arr.Length; ` `    `  `        ``Console.WriteLine(sumModArr(arr, n)); ` `    ``} ` `}`   `// This code is contributed by 29AjayKumar`

## Javascript

 ``

Output:

`8`

Time Complexity: O(N)
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!