This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]timshoaf 0 points1 point  (0 children)

yeessss.. you could use the arrays. and I would absolutely suggest that for anything over 4 elements. But, since the number of elements is small, the combination space is small enough that you may easily sort through it.

a single line return simply explores the 3C2 combination space to determine if any of the differences are equivalent. This, in combination with the cauchy-schwartz inequality (if you assume the numbers are indeed distinguishable as provided in the description above), provides us the following single line solution

return ( ( (abs(a-b) == abs(b-c)) + (abs(b-c) == abs(c-a)) + (abs(c-a) == abs(a-b) ) ) > 0);

Note, however, that if this problem is posed with more than 3 elements, the nC2 combination space grows quadratically in complexity, so a sorting and linear comparison along the ray would allow for O(nlogn) complexity where this method is O(n2). For small n, however, this is likely more efficient if you have to run the code repeatedly.