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)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay