Find maximum of two numbers without using comparison operators or if-else
MediumUpdated: Sep 20, 2025
Problem
Write a method which finds the maximum of two numbers You should not use if-else or any other comparison operator
Examples
Example 1
Input: a = 5, b = 10
Output: 10
Solution
Method 1 - Arithmetic midpoint (overflow-aware)
Intuition
The average-based formula
ans = (a + b) / 2 + |a - b| / 2
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.
Approach
- Use 64-bit intermediate arithmetic to avoid overflow for 32-bit inputs.
- Compute
sum = (long long)a + (long long)b,diff = llabs((long long)a - (long long)b). - ans =
sum/2 + diff/2. - For
min, usesum/2 - diff/2. - 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.
Code
C++
class Solution {
public:
int getMax(int a, int b) {
long long s = static_cast<long long>(a) + static_cast<long long>(b);
long long d = llabs(static_cast<long long>(a) - static_cast<long long>(b));
int ans = static_cast<int>(s / 2 + d / 2);
return ans;
}
int getMin(int a, int b) {
long long s = static_cast<long long>(a) + static_cast<long long>(b);
long long d = llabs(static_cast<long long>(a) - static_cast<long long>(b));
int ans = static_cast<int>(s / 2 - d / 2);
return ans;
}
};
Go
package main
func getMax(a, b int) int {
var s int64 = int64(a) + int64(b)
var d int64
if a >= b { d = int64(a - b) } else { d = int64(b - a) }
ans := int(s/2 + d/2)
return ans
}
func getMin(a, b int) int {
var s int64 = int64(a) + int64(b)
var d int64
if a >= b { d = int64(a - b) } else { d = int64(b - a) }
ans := int(s/2 - d/2)
return ans
}
Java
class Solution {
public int getMax(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;
}
public int getMin(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;
}
}
Kotlin
class Solution {
fun getMax(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
}
fun getMin(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
}
}
Python
class Solution:
def getMax(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
def getMin(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
Complexity
- ⏰ Time complexity:
O(1), constant-time arithmetic operations for addition, subtraction and division. - 🧺 Space complexity:
O(1), constant extra space for temporaries.
Method 2 - Sign-bit extraction (MSB technique)
Intuition
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.
Approach
- Compute
c = a - busing an integer type that has the same width as inputs. - Extract the sign bit into
kas an unsigned 0/1 (careful with right-shift semantics and signedness). - ans_max =
a - k * c. - ans_min =
b + k * c(equivalentlya - (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 - bis outside the signed range for the chosen type.
Code
C++
class Solution {
public:
int getMax(int a, int b) {
int c = a - b;
unsigned int uc = static_cast<unsigned int>(c);
int k = static_cast<int>((uc >> 31) & 0x1u);
int ans = a - k * c;
return ans;
}
int getMin(int a, int b) {
int c = a - b;
unsigned int uc = static_cast<unsigned int>(c);
int k = static_cast<int>((uc >> 31) & 0x1u);
int ans = b + k * c;
return ans;
}
};
Go
package main
func getMax(a, b int) int {
c := a - b
// convert to unsigned to perform logical right shift on sign bit
uc := uint32(int32(c))
k := int((uc >> 31) & 0x1)
ans := a - k*c
return ans
}
func getMin(a, b int) int {
c := a - b
uc := uint32(int32(c))
k := int((uc >> 31) & 0x1)
ans := b + k*c
return ans
}
Java
class Solution {
public int getMax(int a, int b) {
int c = a - b;
int k = (c >> 31) & 0x1;
int ans = a - k * c;
return ans;
}
public int getMin(int a, int b) {
int c = a - b;
int k = (c >> 31) & 0x1;
int ans = b + k * c;
return ans;
}
}
Kotlin
class Solution {
fun getMax(a: Int, b: Int): Int {
val c = a - b
val k = (c shr 31) and 0x1
val ans = a - k * c
return ans
}
fun getMin(a: Int, b: Int): Int {
val c = a - b
val k = (c shr 31) and 0x1
val ans = b + k * c
return ans
}
}
Python
class Solution:
def getMax(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
def getMin(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
Complexity
- ⏰ Time complexity:
O(1), constant-time arithmetic and bit operations. - 🧺 Space complexity:
O(1), constant extra space for a few temporaries.