Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


That's actually the kind of solution I thought the author of the article was going to suggest, before I read their solution.

On an unrelated note, why do you use i+1<length rather than i<length-1 ?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: