Open vamsireddy4110 opened 1 year ago
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; start = temp; } if(end>=arr.length) end=arr.length-1;
The problem you are facing is that you are increasing the sizeofbox*2 which is leading to the out of index, just add a condition if it goes out of bound set it to the last index of the array.
https://github.com/kunal-kushwaha/DSA-Bootcamp-Java/blob/74bd46486cfc2d62c13fe1b26d4fe870b7bb8a20/lectures/10-binary%20search/code/src/com/kunal/InfiniteArray.java#L7C1-L7C9
for below target you will get "Index 13 out of bounds for length 11" int target = 160;