kunal-kushwaha / DSA-Bootcamp-Java

This repository consists of the code samples, assignments, and notes for the Java data structures & algorithms + interview preparation bootcamp of WeMakeDevs.
https://www.youtube.com/playlist?list=PL9gnSGHSqcnr_DxHsP7AW9ftq0AtAyYqJ
17.24k stars 11.07k forks source link

Index out of bound issue for highest number search #1159

Open vamsireddy4110 opened 1 year ago

vamsireddy4110 commented 1 year ago

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;

GURDEEP-SINGH01 commented 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.