"Of three points supplied, is one a midpoint of the other two?" Write a method named hasMidpoint that accepts three integers as parameters and returns true if one of the integers is the midpoint between the other two integers; that is, if one integer is exactly halfway between them. Your method should return false if no such midpoint relationship exists.


Here are two different versions of code that I wrote at two different occasions to accomplish the same thing. How can I make my code better, and is there a better way to accomplish this task?

public static boolean hasMidpoint(int a, int b, int c) {
        if ( a == b &amp;&amp; b == c) {
            return true;
        } else if ((a + b) / 2.0 == c || (b + c) / 2.0 == a || (a + c) / 2.0 == b) {
            return true;
        } else {
            return false;
        }
    }


public static boolean hasMidpoint(int p1, int p2, int p3) {
        if((double)(p1 + p2) / 2 == p3 ||  (double) (p2 + p3) / 2 == p1 || (double) (p1 + p3) / 2 == p2){
            return true;
        }
        return false;
    }
All of the previous answers seem to work on averages of each of the 2 numbers (expressed as either division or multiplication). But I'm working on averages of all 3 numbers, on the assertion that the average of 3 numbers would be the midpoint of the min and the max...

It would seem to be fewer operations.

I'm not a Java developer, so be kind if I've missed something....

public static boolean hasMidpoint(int a, int b, int c) {
    int iTotal = a + b + c;
    // I took mleyfman's idea and replaced division with multiplication
    return (3 * a == iTotal) || (3 * b == iTotal) || (3 * c == iTotal);
}


Edit: Implementing suggestions, the code is now:

public static boolean hasMidpoint(int a, int b, int c) {
    long total = a + b + c;
    // I took mleyfman's idea and replaced division with multiplication
    // A midpoint will always be the average of the 3 values, so 3*midpoint must equal sum of a, b and c.
    return (3 * a == total) || (3 * b == total) || (3 * c == total);
}