returns the larger of a and b. It follows from decomposing max(a,b) as midpoint plus half the absolute difference. This is simple and branch-free, but naive use can overflow when a + b or |a - b| exceed the integer range.
Edge cases: works for negative numbers, equal numbers, and handles typical 32-bit ranges when using 64-bit intermediates; still document limits for extreme inputs beyond 64-bit.
classSolution {
public:int getMax(int a, int b) {
longlong s =static_cast<longlong>(a) +static_cast<longlong>(b);
longlong d = llabs(static_cast<longlong>(a) -static_cast<longlong>(b));
int ans =static_cast<int>(s /2+ d /2);
return ans;
}
intgetMin(int a, int b) {
longlong s =static_cast<longlong>(a) +static_cast<longlong>(b);
longlong d = llabs(static_cast<longlong>(a) -static_cast<longlong>(b));
int ans =static_cast<int>(s /2- d /2);
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
packagemainfuncgetMax(a, bint) int {
varsint64 = int64(a) + int64(b)
vardint64ifa>=b { d = int64(a-b) } else { d = int64(b-a) }
ans:= int(s/2+d/2)
returnans}
funcgetMin(a, bint) int {
varsint64 = int64(a) + int64(b)
vardint64ifa>=b { d = int64(a-b) } else { d = int64(b-a) }
ans:= int(s/2-d/2)
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
publicintgetMax(int a, int b) {
long s = (long) a + (long) b;
long d = Math.abs((long) a - (long) b);
int ans = (int) (s / 2 + d / 2);
return ans;
}
publicintgetMin(int a, int b) {
long s = (long) a + (long) b;
long d = Math.abs((long) a - (long) b);
int ans = (int) (s / 2 - d / 2);
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
fungetMax(a: Int, b: Int): Int {
val s = a.toLong() + b.toLong()
val d = kotlin.math.abs(a.toLong() - b.toLong())
val ans = (s / 2 + d / 2).toInt()
return ans
}
fungetMin(a: Int, b: Int): Int {
val s = a.toLong() + b.toLong()
val d = kotlin.math.abs(a.toLong() - b.toLong())
val ans = (s / 2 - d / 2).toInt()
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution:
defgetMax(self, a: int, b: int) -> int:
s: int = int(a) + int(b)
d: int = abs(int(a) - int(b))
ans: int = int(s //2+ d //2)
return ans
defgetMin(self, a: int, b: int) -> int:
s: int = int(a) + int(b)
d: int = abs(int(a) - int(b))
ans: int = int(s //2- d //2)
return ans
Compute c = a - b. The sign bit of c indicates whether a < b (sign bit 1) or a >= b (sign bit 0) in two’s-complement representation. Use that bit as k (0 or 1) and return a - k * c which equals max(a,b). This is branchless and works on fixed-width two’s-complement integers.
Compute c = a - b using an integer type that has the same width as inputs.
Extract the sign bit into k as an unsigned 0/1 (careful with right-shift semantics and signedness).
ans_max = a - k * c.
ans_min = b + k * c (equivalently a - (1-k) * c).
Edge cases: behavior depends on two’s-complement representation and defined shift behavior — use unsigned shifts or cast to unsigned to read the high bit portably. Beware of overflow when a - b is outside the signed range for the chosen type.
classSolution {
public:int getMax(int a, int b) {
int c = a - b;
unsignedint uc =static_cast<unsignedint>(c);
int k =static_cast<int>((uc >>31) &0x1u);
int ans = a - k * c;
return ans;
}
intgetMin(int a, int b) {
int c = a - b;
unsignedint uc =static_cast<unsignedint>(c);
int k =static_cast<int>((uc >>31) &0x1u);
int ans = b + k * c;
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
packagemainfuncgetMax(a, bint) int {
c:=a-b// convert to unsigned to perform logical right shift on sign bituc:= uint32(int32(c))
k:= int((uc>>31) &0x1)
ans:=a-k*creturnans}
funcgetMin(a, bint) int {
c:=a-buc:= uint32(int32(c))
k:= int((uc>>31) &0x1)
ans:=b+k*creturnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
publicintgetMax(int a, int b) {
int c = a - b;
int k = (c >> 31) & 0x1;
int ans = a - k * c;
return ans;
}
publicintgetMin(int a, int b) {
int c = a - b;
int k = (c >> 31) & 0x1;
int ans = b + k * c;
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
fungetMax(a: Int, b: Int): Int {
val c = a - b
val k = (c shr 31) and 0x1val ans = a - k * c
return ans
}
fungetMin(a: Int, b: Int): Int {
val c = a - b
val k = (c shr 31) and 0x1val ans = b + k * c
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution:
defgetMax(self, a: int, b: int) -> int:
c: int = int(a) - int(b)
# mask to 32 bits then shift to obtain sign bit as 0/1 uc: int = c &0xFFFFFFFF k: int = (uc >>31) &0x1 ans: int = int(a) - k * c
return ans
defgetMin(self, a: int, b: int) -> int:
c: int = int(a) - int(b)
uc: int = c &0xFFFFFFFF k: int = (uc >>31) &0x1 ans: int = int(b) + k * c
return ans