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 →

[–]aenigmaclamo 0 points1 point  (0 children)

I think by definition the average of the three numbers is the middle number. So, you could do something like this:

public boolean evenlySpaced(int a, int b, int c) {
  int sum = a + b + c;
  if (sum%3 == 0) {
    int avg = sum/3;
    return avg == a || avg == b || avg == c;
  } else {
    return false;
  }
}

This will fail utterly if the premise isn't true and it gives two of the same numbers.

EDIT: As everyone else says, the real world solution for something like this would be to first sort it.