Given a, b, c. How to find the minimum of them for two comparisons?

  • four
    d = ((a > b)? b : a); result = (d > c)? c : d; - Igor
  • one
    result = min (min (a, b), c). Each min call is one comparison. - Chorkov
  • Thanks for the help, but I’ve got more readable)) if (a <b) {min = a; } else {min = b; } if (min> c) {min = c; } - Bek Murat

2 answers 2

Thanks for the help, but I got it more readable))

 if (a < b){ min = a; }else{ min = b; } if (min > c){ min = c; } 

    You can absolutely no comparisons:

     private static int Min (int a, int b) { return (a + b - Math.abs(a - b)) / 2; } Min(Min(a, b), c); 
    • Math.abs work without comparisons? - Andrey NOP
    • @AndrewNOP in java source code - no , but in general it is possible without comparisons . - zRrr
    • Well, then either take the library Math.min() right away, or write your full version without using comparisons, otherwise what's the point? - Andrey NOP
    • This is a task for logic, and in the case of using such functions it does not count) - Bek Murat