Skip to content

Commit d241f98

Browse files
committed
fix: mid = (left + right) / 2 to mid = left + (right - left) / 2 for avoding integer overflow and be consistent to the book
1 parent 671d71d commit d241f98

7 files changed

+8
-8
lines changed

java/Binary Search/CuttingWood.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public int cuttingwood(int[] heights, int k) {
77
while (left < right) {
88
// Bias the midpoint to the right during the upper-bound binary
99
// search.
10-
int mid = (left + right) / 2 + 1;
10+
int mid = left + (right - left) / 2 + 1;
1111
if (cutsEnoughWood(mid, k, heights)) {
1212
left = mid;
1313
} else {

java/Binary Search/FindTheInsertionIndex.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ public int findTheInsertionIndex(int[] nums, int target) {
33
int left = 0;
44
int right = nums.length;
55
while (left < right) {
6-
int mid = (left + right) / 2;
6+
int mid = left + (right - left) / 2;
77
// If the midpoint value is greater than or equal to the target,
88
// the lower bound is either at the midpoint, or to its left.
99
if (nums[mid] >= target) {

java/Binary Search/FindTheTargetInARotatedSortedArray.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public int findTheTargetInARotatedSortedArray(int[] nums, int target) {
44
int left = 0;
55
int right = nums.length - 1;
66
while (left < right) {
7-
int mid = (left + right) / 2;
7+
int mid = left + (right - left) / 2;
88
if (nums[mid] == target) {
99
return mid;
1010
}

java/Binary Search/FirstAndLastOccurrencesOfANumber.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ private int lowerBoundBinarySearch(int[] nums, int target) {
1010
int left = 0;
1111
int right = nums.length - 1;
1212
while (left < right) {
13-
int mid = (left + right) / 2;
13+
int mid = left + (right - left) / 2;
1414
if (nums[mid] > target) {
1515
right = mid - 1;
1616
} else if (nums[mid] < target) {
@@ -28,7 +28,7 @@ private int upperBoundBinarySearch(int[] nums, int target) {
2828
int right = nums.length - 1;
2929
while (left < right) {
3030
// In upper-bound binary search, bias the midpoint to the right.
31-
int mid = (left + right) / 2 + 1;
31+
int mid = left + (right - left) / 2 + 1;
3232
if (nums[mid] > target) {
3333
right = mid - 1;
3434
} else if (nums[mid] < target) {

java/Binary Search/LocalMaximaInArray.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ public int localMaximaInArray(int[] nums) {
33
int left = 0;
44
int right = nums.length - 1;
55
while (left < right) {
6-
int mid = (left + right) / 2;
6+
int mid = left + (right - left) / 2;
77
if (nums[mid] > nums[mid + 1]) {
88
right = mid;
99
} else {

java/Binary Search/MatrixSearch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public boolean matrixSearch(int[][] matrix, int target) {
66
int right = m * n - 1;
77
// Perform binary search to find the target.
88
while (left <= right) {
9-
int mid = (left + right) / 2;
9+
int mid = left + (right - left) / 2;
1010
int r = mid / n;
1111
int c = mid % n;
1212
if (matrix[r][c] == target) {

java/Binary Search/WeightedRandomSelection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ private int select() {
1717
// Perform lower-bound binary search to find which endpoint (i.e., prefix
1818
// sum value) corresponds to the target.
1919
while (left < right) {
20-
int mid = (left + right) / 2;
20+
int mid = left + (right - left) / 2;
2121
if (prefixSum[mid] < target) {
2222
left = mid + 1;
2323
} else {

0 commit comments

Comments
 (0)