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}
Brute Force Approach:
The brute force approach to solve this problem is to iterate through the array for each element, calculate the sum of all other elements, and store it in the sum array. This can be done using two nested loops where the outer loop iterates through all the elements, and the inner loop calculates the sum of all other elements.
- Create an empty sum array of size n to store the sum of all elements except the current element.
- Iterate through the array for each element using a for loop.
- Initialize the sum of the current element as 0.
- For each element, iterate through the array using another for loop.
- If the current index of the inner loop is not equal to the index of the outer loop, add the value of the element at the current index to the sum of the current element.
- Store the sum of all other elements in the sum array at the index of the current element.
- Iterate through the sum array and print its elements.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void sumArray( int arr[], int n)
{
int sum[n];
for ( int i=0; i<n; i++){
sum[i] = 0;
for ( int j=0; j<n; j++){
if (j!=i)
sum[i] += arr[j];
}
}
for ( int i=0; i<n; i++)
cout<<sum[i]<< " " ;
}
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.Arrays;
public class Main {
static void sumArray( int [] arr, int n) {
int [] sum = new int [n];
for ( int i = 0 ; i < n; i++) {
sum[i] = 0 ;
for ( int j = 0 ; j < n; j++) {
if (j != i) {
sum[i] += arr[j];
}
}
}
for ( int i = 0 ; i < n; i++) {
System.out.print(sum[i] + " " );
}
}
public static void main(String[] args) {
int [] arr = { 3 , 6 , 4 , 8 , 9 };
int n = arr.length;
sumArray(arr, n);
}
}
|
Python3
def sumArray(arr):
n = len (arr)
sum_result = [ 0 ] * n
for i in range (n):
sum_result[i] = 0
for j in range (n):
if j ! = i:
sum_result[i] + = arr[j]
for i in range (n):
print (sum_result[i])
arr = [ 3 , 6 , 4 , 8 , 9 ]
sumArray(arr)
|
C#
using System;
class Program
{
static void SumArray( int [] arr, int n)
{
int [] sum = new int [n];
for ( int i = 0; i < n; i++)
{
sum[i] = 0;
for ( int j = 0; j < n; j++)
{
if (j != i)
sum[i] += arr[j];
}
}
for ( int i = 0; i < n; i++)
Console.Write(sum[i] + " " );
}
static void Main()
{
int [] arr = { 3, 6, 4, 8, 9 };
int n = arr.Length;
SumArray(arr, n);
}
}
|
Javascript
function sumArray(arr) {
const n = arr.length;
const sum = new Array(n);
for (let i = 0; i < n; i++) {
sum[i] = 0;
for (let j = 0; j < n; j++) {
if (j !== i) {
sum[i] += arr[j];
}
}
}
for (let i = 0; i < n; i++) {
console.log(sum[i]);
}
}
const arr = [3, 6, 4, 8, 9];
sumArray(arr);
|
Time Complexity: O(n^2), as we are using nested loops to iterate through the array.
Space Complexity: O(n) because we are creating an additional array of the same size as the input array to store the sum of all elements except the current element.
Algorithm:
- Construct a temporary array leftSum[] such that leftSum[i] contains sum of all elements on left of arr[i] excluding arr[i].
- Construct another temporary array rightSum[] such that rightSum[i] contains sum of all elements on right of arr[i] excluding arr[i].
- To get sum[], sum left[] and right[].
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void sumArray( int arr[], int n)
{
int leftSum[n], rightSum[n], Sum[n], i, j;
leftSum[0] = 0;
rightSum[n - 1] = 0;
for (i = 1; i < n; i++)
leftSum[i] = arr[i - 1] + leftSum[i - 1];
for (j = n - 2; j >= 0; j--)
rightSum[j] = arr[j + 1] + rightSum[j + 1];
for (i = 0; i < n; i++)
Sum[i] = leftSum[i] + rightSum[i];
for (i = 0; i < n; i++)
cout << Sum[i] << " " ;
}
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 leftSum[] = new int [n];
int rightSum[] = new int [n];
int Sum[] = new int [n];
int i = 0 , j = 0 ;
leftSum[ 0 ] = 0 ;
rightSum[n - 1 ] = 0 ;
for (i = 1 ; i < n; i++)
leftSum[i] = arr[i - 1 ] + leftSum[i - 1 ];
for (j = n - 2 ; j >= 0 ; j--)
rightSum[j] = arr[j + 1 ] + rightSum[j + 1 ];
for (i = 0 ; i < n; i++)
Sum[i] = leftSum[i] + rightSum[i];
for (i = 0 ; i < n; i++)
System.out.print(Sum[i] + " " );
}
public static void main(String[] args)
{
int arr[] = { 3 , 6 , 4 , 8 , 9 };
int n = arr.length;
sumArray(arr, n);
}
}
|
Python3
def sumArray(arr, n):
leftSum = [ 0 for i in range (n)]
rightSum = [ 0 for i in range (n)]
Sum = [ 0 for i in range (n)]
i, j = 0 , 0
leftSum[ 0 ] = 0
rightSum[n - 1 ] = 0
for i in range ( 1 , n):
leftSum[i] = arr[i - 1 ] + leftSum[i - 1 ]
for j in range (n - 2 , - 1 , - 1 ):
rightSum[j] = arr[j + 1 ] + rightSum[j + 1 ]
for i in range ( 0 , n):
Sum [i] = leftSum[i] + rightSum[i]
for i in range (n):
print ( Sum [i], end = " " )
arr = [ 3 , 6 , 4 , 8 , 9 ]
n = len (arr)
sumArray(arr, n)
|
C#
using System ;
class Geeks {
public static void sumArray( int []arr, int n)
{
int []leftSum = new int [n];
int []rightSum = new int [n];
int []Sum = new int [n];
int i = 0, j = 0;
leftSum[0] = 0;
rightSum[n - 1] = 0;
for (i = 1; i < n; i++)
leftSum[i] = arr[i - 1] + leftSum[i - 1];
for (j = n - 2; j >= 0; j--)
rightSum[j] = arr[j + 1] + rightSum[j + 1];
for (i = 0; i < n; i++)
Sum[i] = leftSum[i] + rightSum[i];
for (i = 0; i < n; i++)
Console.Write(Sum[i] + " " );
}
public static void Main()
{
int []arr = { 3, 6, 4, 8, 9 };
int n = arr.Length;
sumArray(arr, n);
}
}
|
Javascript
<script>
function sumArray(arr, n)
{
let leftSum = new Array(n);
let rightSum = new Array(n);
let Sum = new Array(n);
let i = 0, j = 0;
leftSum[0] = 0;
rightSum[n - 1] = 0;
for (i = 1; i < n; i++)
leftSum[i] = arr[i - 1] + leftSum[i - 1];
for (j = n - 2; j >= 0; j--)
rightSum[j] = arr[j + 1] + rightSum[j + 1];
for (i = 0; i < n; i++)
Sum[i] = leftSum[i] + rightSum[i];
for (i = 0; i < n; i++)
document.write(Sum[i] + " " );
}
let arr = [ 3, 6, 4, 8, 9 ];
let n = arr.length;
sumArray(arr, n);
</script>
|
PHP
<?php
function sumArray( $arr , $n )
{
$leftSum = array_fill (0, $n , 0);
$rightSum = array_fill (0, $n , 0);
$Sum = array_fill (0, $n , 0);
$leftSum [0] = 0;
$rightSum [ $n - 1] = 0;
for ( $i = 1; $i < $n ; $i ++)
$leftSum [ $i ] = $arr [ $i - 1] +
$leftSum [ $i - 1];
for ( $j = $n - 2; $j >= 0; $j --)
$rightSum [ $j ] = $arr [ $j + 1] +
$rightSum [ $j + 1];
for ( $i = 0; $i < $n ; $i ++)
$Sum [ $i ] = $leftSum [ $i ] + $rightSum [ $i ];
for ( $i = 0; $i < $n ; $i ++)
echo $Sum [ $i ]. " " ;
}
$arr = array ( 3, 6, 4, 8, 9 );
$n = count ( $arr );
sumArray( $arr , $n );
?>
|
Complexity Analysis:
- Time Complexity: O(n)
- Space Complexity: O(n)
The above method can be optimized to work in O(1) auxiliary space.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void sumArray( int arr[], int n)
{
int i, temp = 0;
int Sum[n];
memset (Sum, 0, n);
for (i = 0; i < n; i++) {
Sum[i] = temp;
temp += arr[i];
}
temp = 0;
for (i = n - 1; i >= 0; i--) {
Sum[i] += temp;
temp += arr[i];
}
for (i = 0; i < n; i++)
cout << Sum[i] << " " ;
}
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 );
for (i = 0 ; i < n; i++) {
Sum[i] = temp;
temp += arr[i];
}
temp = 0 ;
for (i = n - 1 ; i >= 0 ; i--) {
Sum[i] += temp;
temp += arr[i];
}
for (i = 0 ; i < n; i++)
System.out.print(Sum[i] + " " );
}
public static void main(String[] args)
{
int arr[] = { 3 , 6 , 4 , 8 , 9 };
int n = arr.length;
sumArray(arr, n);
}
}
|
Python3
def sumArray(arr, n):
i, temp = 0 , 0
Sum = [ 0 for i in range (n)]
for i in range (n):
Sum [i] = temp
temp + = arr[i]
temp = 0
for i in range (n - 1 , - 1 , - 1 ):
Sum [i] + = temp
temp + = arr[i]
for i in range (n):
print ( Sum [i], end = " " )
arr = [ 3 , 6 , 4 , 8 , 9 ]
n = len (arr)
sumArray(arr, n)
|
C#
using System;
class Geeks {
public static void sumArray( int []arr, int n)
{
int i = 0, temp = 0;
int []Sum = new int [n];
for ( i=0;i<n;i++)
Sum[i] = 0;
for (i = 0; i < n; i++) {
Sum[i] = temp;
temp += arr[i];
}
temp = 0;
for (i = n - 1; i >= 0; i--) {
Sum[i] += temp;
temp += arr[i];
}
for (i = 0; i < n; i++)
Console.Write(Sum[i] + " " );
}
public static void Main()
{
int []arr = { 3, 6, 4, 8, 9 };
int n = arr.Length;
sumArray(arr, n);
}
}
|
Javascript
<script>
function sumArray(arr, n)
{
let i = 0, temp = 0;
let Sum = new Array(n);
for ( i=0;i<n;i++)
Sum[i] = 0;
for (i = 0; i < n; i++) {
Sum[i] = temp;
temp += arr[i];
}
temp = 0;
for (i = n - 1; i >= 0; i--) {
Sum[i] += temp;
temp += arr[i];
}
for (i = 0; i < n; i++)
document.write(Sum[i] + " " );
}
let arr = [ 3, 6, 4, 8, 9 ];
let n = arr.length;
sumArray(arr, n);
</script>
|
PHP
<?php
function sumArray( $arr , $n )
{
$temp = 0;
$Sum = array_fill (0, $n , 0);
for ( $i = 0; $i < $n ; $i ++)
{
$Sum [ $i ] = $temp ;
$temp += $arr [ $i ];
}
$temp = 0;
for ( $i = $n - 1; $i >= 0; $i --)
{
$Sum [ $i ] += $temp ;
$temp += $arr [ $i ];
}
for ( $i = 0; $i < $n ; $i ++)
echo $Sum [ $i ] . " " ;
}
$arr = array ( 3, 6, 4, 8, 9 );
$n = count ( $arr );
sumArray( $arr , $n );
?>
|
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(n)
Last Updated :
26 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...