Question: How to use Java binary search to solve it? The only condition to be a Unique index in array Arr[O .. n-1] is to have
How to use Java binary search to solve it?
The only condition to be a Unique index in array Arr[O .. n-1] is to have an index such that Arr[i] = i. Given a sorted array of distinct integers, write a method to find this unique index provided it exists in the array Arr. Please do not submit the simple solution of going through each element of the array one by one to find the unique index. Actual Number -10 -5-112 3 5 7 9 12 15e Index* 0 2 3 45 6 7 8 9 10 Hint: In binary search, we find an element k by comparing it to the middle element, x , and determining if k would land on the left or the right side of x. Building off this idea, is there a way that we can look at the middle element to determine where a unique index might be
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
