# LeetCode: Search Insert Position Solution

Understand why returns "low/left"

## Approach

Goal of this problem is understanding classic binary search pattern in a different way: why returns low/left?

## Implementation

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}/**2 * @param {number[]} nums3 * @param {number} target4 * @return {number}5 */6var searchInsert = function (nums, target) {7  let N = nums.length8  let left = 09  let right = N - 110  let res = null11
12  while (left <= right) {13    let mid = Math.floor((left + right) / 2)14    if (nums[mid] > target) {15      right = mid - 116    } else if (nums[mid] < target) {17      left = mid + 118    } else {19      res = mid20      break21    }22  }23
24  return res !== null ? res : left25}```

leetcode

array

binary search

## Next Post

Aug 12, 2021

Same as LC 141, but different in return