> For the complete documentation index, see [llms.txt](https://hadyang.gitbook.io/interview/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hadyang.gitbook.io/interview/content/docs/offer/findnumberswithsum/index.md).

# index

***

### title: 和为 date: 2019-08-21T11:00:41+08:00 draft: false categories: offer

### 题目

[牛客网](https://www.nowcoder.com/practice/390da4f7a00f44bea7c2f3d19491311b?tpId=13\&tqId=11195\&tPage=3\&rp=2\&ru=%2Fta%2Fcoding-interviews\&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking)

输入一个递增排序的数组和一个数字S，在数组中查找两个数，使得他们的和正好是S，如果有多对数字的和等于S，输出两个数的乘积最小的。

> 对应每个测试案例，输出两个数，小的先输出。

### 解题思路

1. 利用二分查找的思想，由于是排序数组，通过两个指针来进行遍历

```
public ArrayList<Integer> FindNumbersWithSum(int[] array, int sum) {
    ArrayList<Integer> res = new ArrayList<>();

    if (array == null || array.length == 1) {
        return res;
    }

    int start = 0, end = array.length - 1;
    int minMulti = Integer.MAX_VALUE;
    int a = -1, b = -1;

    while (start < end) {
        int t = array[start] + array[end];
        if (t == sum) {
            int multi = array[start] * array[end];
            if (multi < minMulti) {
                a = array[start];
                b = array[end];
                minMulti = multi;
            }
            start++;
            end--;
        } else if (t > sum) end--;
        else start++;
    }

    if (a == -1 || b == -1) {
        return res;
    }

    res.add(a);
    res.add(b);

    return res;
}
```


---

# 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://hadyang.gitbook.io/interview/content/docs/offer/findnumberswithsum/index.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.
