> For the complete documentation index, see [llms.txt](https://hannahpun.gitbook.io/leetcode-note/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hannahpun.gitbook.io/leetcode-note/binary-search/1064-fixed-point.md).

# # 1064 Fixed Point (有圖)

[LeetCode](https://leetcode.com/problems/fixed-point/)

```
Given an array A of distinct integers sorted in ascending order, 
return the smallest index i that satisfies A[i] == i.  
Return -1 if no such i exists.
給一個裡面都包含不同數字的陣列 A，排序已經由小到大排好了。請找出符合 A[i] = i 的值
```

```
Example 1:

Input: [-10,-5,0,3,7]
Output: 3
Explanation: 
For the given array, A[0] = -10, A[1] = -5, A[2] = 0, A[3] = 3, thus the output is 3.
Example 2:

Input: [0,2,5,8,17]
Output: 0
Explanation: 
A[0] = 0, thus the output is 0.
Example 3:

Input: [-10,-5,3,4,7,9]
Output: -1
Explanation: 
There is no such i that A[i] = i, thus the output is -1.

/**
 * @param {number[]} A
 * @return {number}
 */
var fixedPoint = function(A) {
    
};
```

### Edge Case

* 可能會有兩種以上答案，例如這種 \[-10, -5, -2, 0, 4, 5, 6, 7, 8, 9, 10]，題目說選最小的為 output&#x20;

### 哪種資料結構解

有順序姓而且明顯是在搜尋，所以會用 Array，而且 input 有順序而且又是找東西，所以會用 Binary Search

### 怎麼解

這題比較特別的是有可能會有一種以上答案，所以會先定義 minIndex 存最小的值。

```
let minIndex = Number.MAX_SAFE_INTEGER;
```

其他部分單純用 Binary Search 解

![](/files/-LqtUPZcFBo6znA3tdSt)

```
mid = Math.floor((start + end) / 2); // 從中間開始找
if (mid < A[mid]) { // 2 < 0
    // 往左找      
    end = mid - 1;
} else if (mid > A[mid]) { // 2 > 0
    // 往右找
    start = mid + 1 // start = 3
} else {
    minIndex = Math.min(minIndex, mid);
    end = mid - 1;
};
```

![](/files/-LqtUV8U_EyFjxBi_Rjr)

```
var fixedPoint = function (A) {
    const len = A.length;
    let minIndex = Number.MAX_SAFE_INTEGER;
    if (len < 1) return -1;

    // 這邊都以 index 為單位
    let start = 0;
    let end = len - 1;

    //  從中間開始切
    let mid;

    while (start <= end) {
        mid = Math.floor((start + end) / 2);
        console.log('mid: ' + mid)
        if (mid < A[mid]) { 
            // 往左找      
            end = mid - 1;
        } else if (mid > A[mid]) { 
            // 往右找
            start = mid + 1 
        } else {
            minIndex = Math.min(minIndex, mid);
            end = mid - 1;
        };
    }
    // 如果上面都不符合代表找不到
    return (minIndex == Number.MAX_SAFE_INTEGER) ? -1 : minIndex;

};


console.log(fixedPoint([-10, -5, -2, 0, 4, 5, 6, 7, 8, 9, 10]))
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hannahpun.gitbook.io/leetcode-note/binary-search/1064-fixed-point.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
