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🤗

Oldest comments (0)

Timeless DEV post...

Git Concepts I Wish I Knew Years Ago

The most used technology by developers is not Javascript.

It's not Python or HTML.

It hardly even gets mentioned in interviews or listed as a pre-requisite for jobs.

I'm talking about Git and version control of course.

One does not simply learn git