Given an array x of size n, construct an array y of size n, where yi = sum(a) - xi.
How can this be done in O(n) using constant space and without using the subtraction operator? I can’t figure this one out. No bitwise operations can be used to mimic subtraction. I know the key here is with the additional constant space by making use of some data structure but how can this be done with the O(n) restriction? Making an array that holds the sums of all combinations except xi would require O(n^2).
So, we have array x, and we must make array y. Let’s assume that our array x have size 5 (just for example). So let’s write values of each element of y’ array.
Do you see this diagonal line? It divides y to left part and right part, where each next element of yLeft is sum of previous yLeft element and some element of x array. The same situation with right part of y (just reversed)
Code, C#: