Valarmathi

Valarmathi

  • NA
  • 37
  • 11.3k

Transform the integer array using c#

Aug 25 2015 7:48 AM

Given an array of integers,

  output an another integer array of equal length such that the element at index 'i' in the output array is the sum of all elements in the input array except for the element at 'i'.

In other words, let inputArray by an integer array of length 'n'. The solution, computed into outputArray, would be:

 

for each j from 1 to n­2:

outputArr[ j ] = inputArray[0] + inputArray[1] + inputArray[2] + ... + inputArray[j­1] + inputArray[j+1] + inputArray[j+2] +...+ inputArray[n­1]

for j = 0:

outputArray[0] = inputArray[1] + outputArray[2] + ... + outputArray[n­1]

for j = n­1

outputArray[n­1] = outputArray[0] + outputArray[1] + outputArray[2] + ... + outputArray[n­2]

For example, the inputArray: { 1, 2, 3, 4 }

returns outputArray: { 9, 8, 7, 6 }

which is nothing but { 2+3+4, 1+3+4, 1+2+4, 1+2+3 }.

Notes:

The input array size will always have at least two elements in it, that is, n >= 2.

You could treat all the numbers as integers

The maximum length of input array is 100.


 

Answers (11)