We can determine the maximum without conditionals by using bit manipulation to extract the sign of the difference between two numbers. The key insight is that if a - b is positive, then a is greater; otherwise, b is greater. We can extract the sign bit and use it as a selector to choose the correct number.
classSolution {
public:int findMax(int a, int b) {
int diff = a - b;
int sign = (diff >>31) &1;
return a * (1- sign) + b * sign;
}
// More robust version handling overflow
intfindMaxRobust(int a, int b) {
longlong diff = (longlong)a - (longlong)b;
int sign = (diff >>63) &1;
return a * (1- sign) + b * sign;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
funcfindMax(a, bint) int {
diff:=a-bsign:= (diff>>31) &1returna*(1-sign) +b*sign}
// More robust version handling overflowfuncfindMaxRobust(a, bint) int {
diff:= int64(a) - int64(b)
sign:= int((diff>>63) &1)
returna*(1-sign) +b*sign}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
publicintfindMax(int a, int b) {
int diff = a - b;
int sign = (diff >>> 31) & 1;
return a * (1 - sign) + b * sign;
}
// More robust version handling overflowpublicintfindMaxRobust(int a, int b) {
long diff = (long)a - (long)b;
int sign = (int)((diff >>> 63) & 1);
return a * (1 - sign) + b * sign;
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution:
deffind_max(self, a: int, b: int) -> int:
diff = a - b
# Python handles arbitrary precision, but we simulate 32-bit behavior sign = (diff >>31) &1if diff <0else0return a * (1- sign) + b * sign
deffind_max_robust(self, a: int, b: int) -> int:
diff = a - b
sign =1if diff <0else0return a * (1- sign) + b * sign
We can use the mathematical property that max(a, b) = (a + b + |a - b|) / 2. This formula works because when a > b, |a - b| = a - b, so we get (a + b + a - b) / 2 = a. When b > a, |a - b| = b - a, so we get (a + b + b - a) / 2 = b.
classSolution {
public:int findMax(int a, int b) {
int sum = a + b;
int diff = a - b;
int abs_diff = (diff + (diff >>31)) ^ (diff >>31);
return (sum + abs_diff) >>1;
}
// Alternative using multiplication to avoid potential overflow
intfindMaxSafe(int a, int b) {
longlong sum = (longlong)a + b;
longlong diff = (longlong)a - b;
longlong abs_diff = diff <0?-diff : diff;
return (int)((sum + abs_diff) >>1);
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
funcfindMax(a, bint) int {
sum:=a+bdiff:=a-babsDiff:= (diff+ (diff>>31)) ^ (diff>>31)
return (sum+absDiff) >>1}
// Alternative using int64 to avoid overflowfuncfindMaxSafe(a, bint) int {
sum:= int64(a) + int64(b)
diff:= int64(a) - int64(b)
varabsDiffint64ifdiff < 0 {
absDiff = -diff } else {
absDiff = diff }
return int((sum+absDiff) >>1)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
publicintfindMax(int a, int b) {
int sum = a + b;
int diff = a - b;
int absDiff = (diff + (diff >> 31)) ^ (diff >> 31);
return (sum + absDiff) >> 1;
}
// Alternative using long to avoid overflowpublicintfindMaxSafe(int a, int b) {
long sum = (long)a + b;
long diff = (long)a - b;
long absDiff = diff < 0 ?-diff : diff;
return (int)((sum + absDiff) >> 1);
}
}
1
2
3
4
5
6
7
8
9
classSolution:
deffind_max(self, a: int, b: int) -> int:
sum_val = a + b
diff = a - b
abs_diff = (diff + (diff >>31)) ^ (diff >>31) if diff <0else diff
return (sum_val + abs_diff) >>1deffind_max_simple(self, a: int, b: int) -> int:
return (a + b + abs(a - b)) //2
We can convert the comparison result into a boolean value (0 or 1) and use it as a multiplier. The trick is to use the fact that (a >= b) can be represented as !(a < b), and we can determine a < b by checking if a - b is negative.
classSolution {
public:int findMax(int a, int b) {
// Check if a >= b without direct comparison
int diff = a - b;
int is_a_greater =1- ((diff >>31) &1);
return a * is_a_greater + b * (1- is_a_greater);
}
// Using XOR for selection
intfindMaxXor(int a, int b) {
int diff = a - b;
int mask = diff >>31; // -1 if a < b, 0 if a >= b
return a ^ ((a ^ b) & mask);
}
};
1
2
3
4
5
6
7
8
9
10
11
12
funcfindMax(a, bint) int {
diff:=a-bisAGreater:=1- ((diff>>31) &1)
returna*isAGreater+b*(1-isAGreater)
}
// Using XOR for selectionfuncfindMaxXor(a, bint) int {
diff:=a-bmask:=diff>>31// -1 if a < b, 0 if a >= breturna ^ ((a ^ b) &mask)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
publicintfindMax(int a, int b) {
int diff = a - b;
int isAGreater = 1 - ((diff >>> 31) & 1);
return a * isAGreater + b * (1 - isAGreater);
}
// Using XOR for selectionpublicintfindMaxXor(int a, int b) {
int diff = a - b;
int mask = diff >> 31; // -1 if a < b, 0 if a >= breturn a ^ ((a ^ b) & mask);
}
}
1
2
3
4
5
6
7
8
9
10
classSolution:
deffind_max(self, a: int, b: int) -> int:
diff = a - b
is_a_greater =1- (1if diff <0else0)
return a * is_a_greater + b * (1- is_a_greater)
deffind_max_xor(self, a: int, b: int) -> int:
diff = a - b
mask =-1if diff <0else0return a ^ ((a ^ b) & mask)