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)

DEV

Thank you.

ย 
Thanks for visiting DEV, weโ€™ve worked really hard to cultivate this great community and would love to have you join us. If youโ€™d like to create an account, you can sign up here.