Skip to content

Commit e5326ec

Browse files
fix: improve binary search mid calculation to avoid overflow
1 parent 5c39e87 commit e5326ec

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

Search/BinarySearch.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
*/
99

1010
function binarySearchRecursive(arr, x, low = 0, high = arr.length - 1) {
11-
const mid = Math.floor(low + (high - low) / 2)
11+
// Using optimized mid calculation to prevent overflow
12+
const mid = low + Math.floor((high - low) / 2);
1213

1314
if (high >= low) {
1415
if (arr[mid] === x) {

0 commit comments

Comments
 (0)