# LeetCode: First Bad Version Solution

Luckily, mid calculation in JS did not throw overflow error

## Approach

Example 1

```.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;}1 2 3 4 52G G G B B3L   M   H4
51 2 3 4 56G G G B B7      L H```

Example 2

```11 2 3 4 52G B B B B3L   M   H4
51 2 3 4 56G B B B B7L M H8
91 2 3 4 510G B B B B11  L H```

## Implementation

```1var solution = function (isBadVersion) {2  return function (n) {3    let lo = 14    let hi = n5
6    while (lo < hi) {7      let mid = Math.floor((lo + hi) / 2)8      if (isBadVersion(mid) === false) {9        lo = mid + 110      } else {11        hi = mid12      }13    }14
15    return lo16  }17}```

Original problem

leetcode

binary search

## Next Post

LeetCode: Two Sum II - Input array is sorted

Sep 14, 2021

If trying to explain takes time, go on

## Previous Post

LeetCode: Binary Search

Sep 13, 2021

Classic problem

Search Posts