> For the complete documentation index, see [llms.txt](https://mayanktyagi3111.gitbook.io/interview-prep/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mayanktyagi3111.gitbook.io/interview-prep/greedy/problem-discussion.md).

# Problem discussion

Harshit gave Aahad an array of size N and asked to minimize the difference between the maximum value and minimum value by modifying the array under the condition that each array element either increase or decrease by k(only once).

It seems difficult for Aahad so he asked for your help

**Input Format**

```
The First line contains two space-separated integers: N,K
Next lines contain N space-separated integers denoting elements of the array
```

**Output Format**

```
The output contains a single integer denoting the minimum difference between maximum value and the minimum value in the array
```

**Constraints**

```
1 =< N <= 10^5 
1 <= Ai,K <= 10^9
```

**Sample Input1:**

```
3 6
1 15 10
```

**Sample Output1:**

```
5
```

**Explaination**

```
We change from 1 to 6, 15 to  9 and 10 to 4. Maximum difference is 5 (between 4 and 9). We can't get a lower difference.
```

```java
public class Main {
    public static int getMinDiff(int arr[], int n, int k) {
        if (n == 1)
            return 0;
        Arrays.sort(arr);
        // Original range
        int ans = arr[n - 1] - arr[0];
        int small = arr[0] + k, big = arr[n - 1] - k;
        // Swap if required
        if (small > big) {
            int temp = small;
            small = big;
            big = temp;
        }
        for (int i = 1; i < n - 1; i++) {
            // We have to do atleast one operation
            int subtsract = arr[i] - k;
            int add = arr[i] + k;
            // If the added / substracted values are within range
            if (subtsract >= small || add <= big)
                continue;
            // If (substracted -> big) range is smaller than (smaller -> added)
            if (big - subtsract <= add - small)
                small = subtsract;
            else
                big = add;
        }
        return (int) Math.min(ans, big - small);
    }
}
```


---

# 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://mayanktyagi3111.gitbook.io/interview-prep/greedy/problem-discussion.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.
