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

Exception in infinite array problem with solution. #1127

Open AmiteshVerma24 opened 1 year ago

AmiteshVerma24 commented 1 year ago

Code link: Question link

This question was very well explained by Kunal however there is small issue with it. While finding the range we are checking whether arr[end] < target however when we try to search for an element which is present towards the end of the array there is a case when end > arr.length - 1 which results in Array Index Out Of Bound Exception to fix it we can use try-catch block.

I have attached my solution below Updated code

Varshithays commented 1 year ago

yea ,thank you AmiteshVerma for giving explanation and cleared my doubt.

Sahilkumar19 commented 1 year ago

Code link: Question link

This question was very well explained by Kunal however there is small issue with it. While finding the range we are checking whether arr[end] < target however when we try to search for an element which is present towards the end of the array there is a case when end > arr.length - 1 which results in Array Index Out Of Bound Exception to fix it we can use try-catch block.

I have attached my solution below Updated code Thanks