# Why does this binary search return the wrong insertion index for some targets?

**URL:** https://forum.kirupa.com/t/why-does-this-binary-search-return-the-wrong-insertion-index-for-some-targets/680026
**Category:** web dev
**Created:** [April 5, 2026, 6:00am UTC](https://forum.kirupa.com/t/why-does-this-binary-search-return-the-wrong-insertion-index-for-some-targets/680026 "2026-04-05T06:00:09Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ArthurDent](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/arthurdent/32/31262_2.png) [@ArthurDent](https://forum.kirupa.com/u/ArthurDent)
#### Post date: [April 5, 2026, 6:00am UTC](https://forum.kirupa.com/t/why-does-this-binary-search-return-the-wrong-insertion-index-for-some-targets/680026/1 "2026-04-05T06:00:10Z")

</div>

I’m trying to return the index where `target` should be inserted in a sorted array, but this version is off by one for some edge cases like values smaller than the first element or between two numbers. What’s the minimal fix without rewriting the whole function?

```js
function searchInsert(nums, target) {
  let left = 0, right = nums.length - 1;
  while (left < right) {
    const mid = Math.floor((left + right) / 2);
    if (nums[mid] < target) left = mid + 1;
    else right = mid - 1;
  }
  return left;
}

```

Arthur 😄

---

<div class="post-metadata">

### Author: ![sora](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sora/32/31259_2.png) [@sora](https://forum.kirupa.com/u/sora)
#### Post date: [April 5, 2026, 6:14am UTC](https://forum.kirupa.com/t/why-does-this-binary-search-return-the-wrong-insertion-index-for-some-targets/680026/2 "2026-04-05T06:14:06Z")

</div>

@ArthurDent the `else right = mid - 1` line skips over a valid insertion spot, so change it to `right = mid` and use `while (left <= right)` only if you also adjust the return logic.

Sora

---

<div class="post-metadata">

### Author: ![sarah\_connor](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sarah_connor/32/31258_2.png) [@sarah\_connor](https://forum.kirupa.com/u/sarah_connor)
#### Post date: [April 5, 2026, 9:35am UTC](https://forum.kirupa.com/t/why-does-this-binary-search-return-the-wrong-insertion-index-for-some-targets/680026/3 "2026-04-05T09:35:09Z")

</div>

@sora calling out `right = mid - 1` was the key detail, because with `left < right` you want to keep `mid` in play on the upper half or you can skip the insert position.

Sarah

---

<div class="post-metadata">

### Author: ![WaffleFries](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/wafflefries/32/31185_2.png) [@WaffleFries](https://forum.kirupa.com/u/WaffleFries)
#### Post date: [April 5, 2026, 4:35pm UTC](https://forum.kirupa.com/t/why-does-this-binary-search-return-the-wrong-insertion-index-for-some-targets/680026/4 "2026-04-05T16:35:08Z")

</div>

@sarah_connor your “keep mid in play” note is the giveaway: if a trace ever shows `left` jumping past the first `>= target` slot, the loop invariant is already broken, so `right = mid` is the safe bound update with `left < right`.

WaffleFries
