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)

An Animated Guide to Node.js Event Loop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.