Given an array arr[] of n integers, construct a Sum Array sum[] (of same size) such that sum[i] is equal to the sum of all the elements of arr[] except arr[i]. Solve it without subtraction operator and in O(n).
Examples:
Input : arr[] = {3, 6, 4, 8, 9}
Output : sum[] = {27, 24, 26, 22, 21}Input : arr[] = {4, 5, 7, 3, 10, 1}
Output : sum[] = {26, 25, 23, 27, 20, 29}
Algorithm:
1) Construct a temporary array leftSum[] such that leftSum[i] contains sum of all elements on left of arr[i] excluding arr[i].
2) Construct another temporary array rightSum[] such that rightSum[i] contains sum of all elements on on right of arr[i] excluding arr[i].
3) To get sum[], sum left[] and right[].
C/C++
#include <bits/stdc++.h> using namespace std; void sumArray(int arr[], int n) { /* Allocate memory for temporary arrays leftSum[], rightSum[] and Sum[]*/ int leftSum[n], rightSum[n], Sum[n], i, j; /* Left most element of left array is always 0 */ leftSum[0] = 0; /* Rightmost most element of right array is always 0 */ rightSum[n - 1] = 0; /* Construct the left array*/ for (i = 1; i < n; i++) leftSum[i] = arr[i - 1] + leftSum[i - 1]; /* Construct the right array*/ for (j = n - 2; j >= 0; j--) rightSum[j] = arr[j + 1] + rightSum[j + 1]; /* Construct the sum array using left[] and right[] */ for (i = 0; i < n; i++) Sum[i] = leftSum[i] + rightSum[i]; /* print the constructed prod array */ for (i = 0; i < n; i++) cout << Sum[i] << " "; } /* Driver program to test above functions */int main() { int arr[] = { 3, 6, 4, 8, 9 }; int n = sizeof(arr) / sizeof(arr[0]); sumArray(arr, n); return 0; } |
Java
import java.util.*; import java.lang.*; import java.io.*; class Geeks { public static void sumArray(int arr[], int n) { /* Allocate memory for temporary arrays leftSum[], rightSum[] and Sum[]*/ int leftSum[] = new int[n]; int rightSum[] = new int[n]; int Sum[] = new int[n]; int i = 0, j = 0; /* Left most element of left array is always 0 */ leftSum[0] = 0; /* Right most element of rght array is always 0 */ rightSum[n - 1] = 0; /* Construct the left array*/ for (i = 1; i < n; i++) leftSum[i] = arr[i - 1] + leftSum[i - 1]; /* Construct the right array*/ for (j = n - 2; j >= 0; j--) rightSum[j] = arr[j + 1] + rightSum[j + 1]; /* Construct the sum array using left[] and right[] */ for (i = 0; i < n; i++) Sum[i] = leftSum[i] + rightSum[i]; /*print the sum array*/ for (i = 0; i < n; i++) System.out.print(Sum[i] + " "); } /* Driver function to test above function*/ public static void main(String[] args) { int arr[] = { 3, 6, 4, 8, 9 }; int n = arr.length; sumArray(arr, n); } } |
C#
// C# implementation of above approach using System ; class Geeks { public static void sumArray(int []arr, int n) { /* Allocate memory for temporary arrays leftSum[], rightSum[] and Sum[]*/ int []leftSum = new int[n]; int []rightSum = new int[n]; int []Sum = new int[n]; int i = 0, j = 0; /* Left most element of left array is always 0 */ leftSum[0] = 0; /* Right most element of rght array is always 0 */ rightSum[n - 1] = 0; /* Construct the left array*/ for (i = 1; i < n; i++) leftSum[i] = arr[i - 1] + leftSum[i - 1]; /* Construct the right array*/ for (j = n - 2; j >= 0; j--) rightSum[j] = arr[j + 1] + rightSum[j + 1]; /* Construct the sum array using left[] and right[] */ for (i = 0; i < n; i++) Sum[i] = leftSum[i] + rightSum[i]; /*print the sum array*/ for (i = 0; i < n; i++) Console.Write(Sum[i] + " "); } /* Driver function to test above function*/ public static void Main() { int []arr = { 3, 6, 4, 8, 9 }; int n = arr.Length; sumArray(arr, n); } // This code is contributed by Ryuga } |
27 24 26 22 21
Time Complexity: O(n)
Space Complexity: O(n)
Auxiliary Space: O(n)
The above method can be optimized to work in O(1) auxiliary space.
C/C++
#include <bits/stdc++.h> using namespace std; void sumArray(int arr[], int n) { int i, temp = 0; /* Allocate memory for the sum array */ int Sum[n]; /* Initialize the sum array as 0 */ memset(Sum, 0, n); /* In this loop, temp variable contains sum of elements on left side excluding arr[i] */ for (i = 0; i < n; i++) { Sum[i] = temp; temp += arr[i]; } /* Initialize temp to 0 for sum on right side */ temp = 0; /* In this loop, temp variable contains sum of elements on right side excluding arr[i] */ for (i = n - 1; i >= 0; i--) { Sum[i] += temp; temp += arr[i]; } for (i = 0; i < n; i++) cout << Sum[i] << " "; } /* Driver program to test above function */int main() { int arr[] = { 3, 6, 4, 8, 9 }; int n = sizeof(arr) / sizeof(arr[0]); sumArray(arr, n); return 0; } |
Java
import java.util.*; import java.lang.*; import java.io.*; class Geeks { public static void sumArray(int arr[], int n) { int i = 0, temp = 0; int Sum[] = new int[n]; Arrays.fill(Sum, 0); /* In this loop, temp variable contains sum of elements on left side excluding arr[i] */ for (i = 0; i < n; i++) { Sum[i] = temp; temp += arr[i]; } /* Initialize temp to 0 for sum on right side */ temp = 0; /* In this loop, temp variable contains sum of elements on right side excluding arr[i] */ for (i = n - 1; i >= 0; i--) { Sum[i] += temp; temp += arr[i]; } for (i = 0; i < n; i++) System.out.print(Sum[i] + " "); } /* Driver function to test above function*/ public static void main(String[] args) { int arr[] = { 3, 6, 4, 8, 9 }; int n = arr.length; sumArray(arr, n); } } |
27 24 26 22 21
Time Complexity: O(n)
Auxiliary Space: O(1)
Recommended Posts:
- A Product Array Puzzle
- A product array puzzle | Set 2 (O(1) Space)
- Find original array from encrypted array (An array of sums of other elements)
- Find an element in array such that sum of left array is equal to sum of right array
- Puzzle 19 | (Poison and Rat)
- Puzzle 16 | (100 Doors)
- Solve the Crossword Puzzle
- Puzzle | Message Spreading
- Puzzle | Selling Chocolates
- Egg Dropping Puzzle with 2 Eggs and K Floors
- Puzzle | Weight of Heavy Ball
- How to check if an instance of 8 puzzle is solvable?
- Program for Chocolate and Wrapper Puzzle
- Puzzle | Cut Blue Painted Cube
- Programming puzzle (Assign value without any control statement)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Ryuga



