Given an array of integers arr
, find a fixed point (i.e., an index i
such that arr[i] == i
). More formally, return the lowest index i
such that arr[i] == i
.
If no such index exists, return -1
.
Example 1:
Input: arr = [-10,-5,0,3,5] Output: 3 Explanation: arr[3] == 3.
Example 2:
Input: arr = [0,2,5,8,17] Output: 0 Explanation: arr[0] == 0.
Example 3:
Input: arr = [-10,-5,3,4,7,9] Output: -1 Explanation: There is no index such that arr[i] == i.
Constraints:
1 <= arr.length < 104
-109 <= arr[i] <= 109
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding a fixed point involves checking every single possibility. We essentially go through each element one by one and see if it satisfies a specific condition. If we find an element that fits the condition, we've found our answer.
Here's how the algorithm would work step-by-step:
def fixed_point_brute_force(number_list):
for index in range(len(number_list)):
# We are checking each element if its value equals its index
if number_list[index] == index:
return index
# If no match is found, the list does not have a fixed point
return -1
The problem asks to find a place in a sorted sequence where the value is the same as its position. The trick is to avoid looking at every single position by using a process that rapidly eliminates large sections of the sequence where the solution can't possibly be.
Here's how the algorithm would work step-by-step:
def fixed_point(sorted_sequence):
left_index = 0
right_index = len(sorted_sequence) - 1
while left_index <= right_index:
middle_index = (left_index + right_index) // 2
middle_value = sorted_sequence[middle_index]
# Check if the value at middle_index is a fixed point
if middle_value == middle_index:
return middle_index
# Adjust search space based on middle_value compared to middle_index
if middle_value > middle_index:
right_index = middle_index - 1
# Adjust search space based on middle_value compared to middle_index
else:
left_index = middle_index + 1
# No fixed point was found
return -1
Case | How to Handle |
---|---|
Null or empty input array | Return -1 immediately as a fixed point cannot exist in an empty array. |
Array with one element and it is a fixed point | Check if arr[0] == 0 and return 0 if true, otherwise return -1. |
Array with one element and it is not a fixed point | Check if arr[0] == 0 and return -1 if false. |
Large array where the fixed point is near the end | Binary search ensures logarithmic time complexity regardless of the fixed point's location. |
Array with negative numbers, zero, and positive numbers | Binary search works correctly with mixed positive, negative, and zero values in the sorted array. |
No fixed point exists in the array | Binary search should terminate and return -1 after searching the entire array without finding a match. |
Integer overflow when calculating the middle index in binary search | Calculate the middle index as mid = low + (high - low) / 2 to avoid overflow. |
Array contains extremely large integers | The algorithm is designed for integers of typical size and does not explicitly handle extremely large integers differently; the underlying language and hardware limitations apply. |