# 908.Smallest Range I

**908.Smallest Range I**

> 给定一个整数数组 A，对于每个整数 A\[i]，我们可以选择任意 x 满足 -K <= x <= K，并将 x 加到 A\[i] 中。 在此过程之后，我们得到一些数组 B。 返回 B 的最大值和 B 的最小值之间可能存在的最小差值。

```
 示例 1：

 输入：A = [1], K = 0
 输出：0
 解释：B = [1]
 示例 2：

 输入：A = [0,10], K = 2
 输出：6
 解释：B = [2,8]
 示例 3：

 输入：A = [1,3,6], K = 3
 输出：0
 解释：B = [3,3,3] 或 B = [4,4,4]

提示：
  1 <= A.length <= 10000
  0 <= A[i] <= 10000
  0 <= K <= 10000
```

方法：这题比较简单，直接比较最大值-K和最小值+K后的大小，如果小于则返回0，大于返回差值。

```
class Solution {
public:
  int smallestRangeI(vector<int>& A, int K) {
      int max=A[0],min=A[0];
      for(int i=1;i<A.size();i++)
      {
          if(max<A[i]) max=A[i];
          if(min >A[i]) min=A[i];
      }

       max -=K;
      min +=K;
      if(max <=min) return 0;
      return max-min;
      
  }
};
```


---

# Agent Instructions: 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://dfine.gitbook.io/leetcode/908.smallest_range_i.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.
