How to calculate difference between two points in some values?

251 Views Asked by At

I have 5 values which are -2, -1, 0, 1, 2. I want to calculate difference between two variables which contains the values from these given values.

Suppose I have two variables x and y. Suppose following cases :

x = 0, y = 0 i.e. (x == y) then return value should be 2.

x = 1, y = -1 or vice versa then the return value should be 0.

x = -2, y = 2 or vice versa then the return value should be -2.

I have to use this formula in a program.

1

There are 1 best solutions below

3
On BEST ANSWER

I have solved this by using following formula :

$$ d(x,y) = 2 - |x - y| $$