By arunraj
Problem Statement
Given a input integer array , create a new array such
that the number an index position i is replaced with sum of the numbers at other index positions. For example, Input -> [1,2,4,5] , Output - > [11,10,8,7]
that the number an index position i is replaced with sum of the numbers at other index positions. For example, Input -> [1,2,4,5] , Output - > [11,10,8,7]
public static void sumArr()
{
int [] a = {3,6,8,12,43,67,5,7};
int[] tempArr1 = new int[a.length];
int[] tempArr2 = new int[a.length];
int forwardSum = 0;
int backwardSum = 0;
for(int i=0,j=a.length-1;i<a.length&&j>=0;j--,i++)
{
tempArr1[i]=forwardSum;
forwardSum = forwardSum + a[i];
tempArr2[j]=backwardSum;
backwardSum = backwardSum + a[j];
}
for(int i=0;i<a.length;i++)
{
System.out.print(tempArr1[i]+tempArr2[i]+",");
}
}
{
int [] a = {3,6,8,12,43,67,5,7};
int[] tempArr1 = new int[a.length];
int[] tempArr2 = new int[a.length];
int forwardSum = 0;
int backwardSum = 0;
for(int i=0,j=a.length-1;i<a.length&&j>=0;j--,i++)
{
tempArr1[i]=forwardSum;
forwardSum = forwardSum + a[i];
tempArr2[j]=backwardSum;
backwardSum = backwardSum + a[j];
}
for(int i=0;i<a.length;i++)
{
System.out.print(tempArr1[i]+tempArr2[i]+",");
}
}
Interview Questions: