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

Update InfiniteArray.java #1105

Closed RunAtTekky closed 6 months ago

RunAtTekky commented 1 year ago

fix: ArrayIndexOutOfBoundsException

The previous code wouldn't give solution if the index end becomes greater than the actual length. Lets say in int[] arr = {3, 5, 7, 9, 10, 90, 100, 130, 140, 160, 170}; There are 11 elements in the list. Thus, from index 0 to 10. Lets say we go from 0,1 to 2,5 and then to 6,11 index. Note that in the first two cases the previous code would have given the correct solution but in the last case of 6,11 the code will throw an error because index 11 is not in the array. This code helps to fix that issue.