You basically need to compare a with both b and c (using Math.abs(a-b)
and using Math.abs(a-c)
) and then check if the other values differ by at least 2. Something like this:
public static boolean closeFar(int a, int b, int c) {
return ( (Math.abs(a-b) == 1 && (Math.abs(a-c) >= 2 && Math.abs(b-c) >= 2) ||
(Math.abs(a-c) == 1 && (Math.abs(a-b) >= 2 && Math.abs(b-c) >= 2)))
);
}
Test cases:
System.out.println(closeFar(1,2,10)); //prints true
System.out.println(closeFar(1,2,3)); //prints false
System.out.println(closeFar(4,1,3)); //prints true
2
solved Tried to solve the “Close Far” exercise [closed]