For neighbor-sum, instead of saying "each output value is the sum of its input neighbors", we can say "each input value contributes to the output values of its neighbors."
Then it is natural to iterate over the input and not the output:
for (int i=1; i < length; i++)
output[i-1] += input[i];
for (int i=0; i + 1 < length; i++)
output[i+1] += input[i];
"Invert the problem" is a really powerful general strategy.
Then it is natural to iterate over the input and not the output:
"Invert the problem" is a really powerful general strategy.