mid = (low+high) / 2 vs mid=low+ (high-low) /2

--

In binary search algorithm implementation most of us including me we get used to calculate mid value by mid = (low + high) / 2 which is ok and fine in most cases but during our readings and research sometime we found another formula mid=low+ (high-low) /2 but we did not pay much attention to the difference.

The first time to knew the difference for me was during solving one of the problems in leetcode and I kept getting Time Limit Exceeded without knowing where is the problem until I tried the mid=low+ (high-low) /2 formula.

I recommend to try to solve this problem on Binary search on leetcode then go to this awesome stackoverflow link to read in details difference.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response