diff --git a/lectures/10-binary search/code/src/com/kunal/InfiniteArray.java b/lectures/10-binary search/code/src/com/kunal/InfiniteArray.java index 65bbfd630..c386ae408 100644 --- a/lectures/10-binary search/code/src/com/kunal/InfiniteArray.java +++ b/lectures/10-binary search/code/src/com/kunal/InfiniteArray.java @@ -14,11 +14,15 @@ static int ans(int[] arr, int target) { int end = 1; // condition for the target to lie in the range - while (target > arr[end]) { + while (end < arr.length && target > arr[end]) { int temp = end + 1; // this is my new start // double the box value // end = previous end + sizeofbox*2 end = end + (end - start + 1) * 2; + if (end >= arr.length) { + end = arr.length - 1; // cap end to last index + } + start = temp; start = temp; } return binarySearch(arr, target, start, end);