From e2ad22ace961e24100318bd462dacb100a074fa5 Mon Sep 17 00:00:00 2001 From: Khuzaima Khalid Date: Wed, 3 Sep 2025 00:32:26 +0500 Subject: [PATCH] Fix out-of-bounds in InfiniteArray Add a bounds check and cap end so arr[end] never goes past the array. --- .../10-binary search/code/src/com/kunal/InfiniteArray.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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);