Type like pro

Index

Sum of array except current element

Sum of array except current element

Problem

Replace an array element a(i) with sum(a)-a(i) without using the '-' operator. Where sum(a)=a(0)+a(1)+...+a(n)

Solution

First we will add the numbers from left to right and place it to a new array in such a way so that each element of the new array e(i)=a(0)+a(1)+...+a(i-1). Then we will do the same thing from right to left. In this way the ith element will include all the elements from its left and right but not the element a(i). As we are always doing a sum we don't need a '-' operator.

Code

package dsalgo;

public class ArraySumExceptCurrent {

 public static void main(String[] args) {
  int[]arr={3,1,4,5,3,4,12,3};
  int[]result=new int[arr.length];
  int i=0;
  int sum=0;
  while(i < arr.length){
   int temp=arr[i];
   result[i++]=sum;
   sum+=temp;
  }
  i--;
  sum=0;
  while(i>=0){
   int temp=arr[i];
   result[i--]+=sum;
   sum+=temp;
  }
  for(int j=0; j < result.length; ++j){
   System.out.print(result[j]+", ");
  }
 }

}