Why we use mid= low + (high-low)/2 in Binary Search Algorithms

I saw many times competitive programmer use mid = low + (high- low )/2 .But in many article we read mid = (low+high)/2 .which should we prefer in competitive programming.And why most competitive programmer prefer to use mid = low + (high-low)/2 . Can any anyone Explain

1 Like

Because if we use mid = (low + high)/2 then it might lead to overflow, as (high + low) can exceed range and will eventually lead to overflow. But if we use mid = low + (high - low)/2, then the possibility of overflow becomes none, as if high and low are in the range, then (high - low) will definitely be in range and will not overflow.

1 Like