DEV Community

SAIFULLAH🇮🇳
SAIFULLAH🇮🇳

Posted on

Trick for the Binary Search Data Structure

A short and effective tip for clearing test cases in competitive programming.
As we know when we search the given elements in binary search either iterative or recursive way we need to find mid value.
So to find mid value we simply use
int mid = low + end / 2 but sometimes it will fail one of the given test cases so instead of this use
int mid = low +(end - low) / 2

Why 🤔

Because as we know integers can hold 10^9 values so if we use int mid = low + end / 2
that means 10^9 + 10^9 here it will exceed the memory.

So instead of this we use above method.

Thanks for reading.
Hope it's helpful to you🤗

Top comments (0)