Find max of 3 integers. Tricky solution
16:50 22 Aug 2026

I was looking at a solution online to find the max of 3 integers, x, y and z

The solution is as follows:

result = (x + y + abs(x - y)) / 2;
max = (result + z + abs(result - z)) / 2;

Where is this derived from? It looks like some sort of super-efficient math logic, to avoid using if-else statements. I'd like to understand how it works.

algorithm max