Problem
Given a sorted array of distinct integers, write a function to find the magic index or fixed point in the array, else return -1.
Definition
Magic index or a Fixed point in an array is an index i
in the array A
such that A[i] = i
.
Examples
Example 1:
|
|
Example 2:
|
|
Solution
Method 1 - Linear Search
Naive approach is to do the linear scan and find the magic index in O(n)
.
Code
|
|
Method 2 - Binary Search
Algorithm
- Check the middle element (
mid = (start + end) / 2
) and compares it to its index innums[mid]
. If they are equal (nums[mid] == mid
), returnmid
as fixed point. - If the middle element is greater than its index search the fixed point in the right half of array
- Conversely, if the middle element is less than its index search the fixed point might be in the left half
Why binary search works
Lets analyze case in point 2 in algorithm when mid > nums[mid]
.
We know the array nums
is sorted and the function f(x)
is increasing (meaning f(x) > f(y)
for x > y
).
When mid > nums[mid]
, for eg. mid = 4
, nums[mid] = 3
… searching in left subarray will be useless, because mid
will always be more than nums[mid]
as elements are distinct. So, we just go to right subarray.
Similarly, when mid < nums[mid]
, for eg. mid = 4, nums[2] = 6
, searching in right subarray will be useless, as mid
will never be able to catch up with the nums[i]
values. So, we can only go to left subarray.
Because of choosing just 1 path to find the answer, binary search.
Code
|
|
Complexity
- ⏰ Time complexity:
O(log n)
- 🧺 Space complexity:
O(1)