3 ms·
What's funny is that the safer alternative: int mid = low + ((high - low) / 2); is probably what most of us originally came up with before we saw the short
by _zagj 2y ago
What's funny is that the safer alternative:
int mid = low + ((high - low) / 2);
is probably what most of us originally came up with before we saw the shorter, more elegant, but overflow-prone approach.
- RaftPeople 2y agoI wrote one before I read the article to see if I would hit the bug and yep, I wrote it the safer way. For me, that is the most readable way to do it because it lines up both conceptually and visually with how I'm thinking of the problem.
- commandersaki 2y agoI was once penalised for doing this in a leetcode problem given during an interview. I was told to rethink things about calculating a mid-point from first principles.