542. Fixed Point

Given an array of distinct integers arr, where arr is sorted in ascending order, return the smallest index i that satisfies arr[i] == i.

If there is no such index, return -1.

Example 1:

Input: arr = [-8, -3, 2, 5, 9]

Output: 2

Explanation: arr[2] = 2, thus the output is 2.

Example 2:

Input: arr = [0, 4, 7, 10, 15]

Output: 0

Explanation: arr[0] = 0, thus the output is 0.

Now Your Turn!

Pick the correct output for the given input

Input: arr = [-6, -2, 1, 8, 12]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= arr.length < 10⁴
  • -10⁹ <= arr[i] <= 10⁹

Follow-up: The O(n) solution is very straightforward. Can we do better?

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int fixedPoint(vector<int>& arr) {
// Your code goes here
}
};
Test Case

Input:

Arr