Flip Values to get the opposite

45 Views Asked by At

Not sure of the name of what I need to do, but I used to do it all the time, and now i forget. I have values 1 - 10. I want 10 to become 1 and 1 to become 10. What is the formula to do this again? It is driving me nuts.

Thanks for the responses. My apologies that I don't fully understand your solution. Let's think of this in an excel spreadsheet where column 'A1' = 1, 'A2' = 2 .... 'A10' = 10. What formula needs to be used to make 'A1' = 10, 'A2' = 9..... 'A10' = 1? I imagine some identifier of the maximum value in the range would be necessary. Again, my apologies for not getting what you are saying.

2

There are 2 best solutions below

0
On

Is this a cs question?

If $x = a, y = b$ are integer variables defined in code, the algorithmic way of swapping their data without allocating memory for a temporary variable is to set

$y = y + x$

then

$x = y - x$

then

$y = y - x$

and then you should have $x = b$, $y = a$

0
On

I found the easiest way, and I knew it was easy.

MAX value in array = x

Value in array = y

New value = z

z = x - y