# CHANGEONE7 - Editorial

**URL:** <https://discuss.codechef.com/t/changeone7-editorial/126036>\
**Category:** editorial\
**Tags:** cakewalk, editorial, start260\
**Created:** [October 14, 2026, 4:30pm UTC](https://discuss.codechef.com/t/changeone7-editorial/126036 "2026-10-14T16:30:47Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![iceknight1093](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@iceknight1093](https://discuss.codechef.com/u/iceknight1093)\
**Post date:** [October 14, 2026, 4:30pm UTC](https://discuss.codechef.com/t/changeone7-editorial/126036/1 "2026-10-14T16:30:47Z")

</div>

# PROBLEM LINK:

[Practice](https://www.codechef.com/problems/CHANGEONE7)  
[Contest: Division 1](https://www.codechef.com/START260A/problems/CHANGEONE7)  
[Contest: Division 2](https://www.codechef.com/START260B/problems/CHANGEONE7)  
[Contest: Division 3](https://www.codechef.com/START260C/problems/CHANGEONE7)  
[Contest: Division 4](https://www.codechef.com/START260D/problems/CHANGEONE7)

_ **Author:** _ [raysh07](https://www.codechef.com/users/raysh07)  
_ **Tester:** _ [iceknight1093](https://www.codechef.com/users/iceknight1093)  
_ **Editorialist:** _ [iceknight1093](https://www.codechef.com/users/iceknight1093)

# DIFFICULTY:

TBD

# PREREQUISITES:

None

# PROBLEM:

You have an array A.  
You can change at most one element.  
Minimize \max(A) - \min(A).

# EXPLANATION:

If we change neither \max(A) nor \min(A), then the value of \max(A) - \min(A) won’t change.  
So we should change one of them to try and improve the answer.

Suppose we change \min(A).  
This means \max(A) won’t change - as long as we don’t make the new element larger than \max(A).  
So one option is to change \min(A) equal to \max(A).  
After this change, the new minimum element of the array will be the second smallest element of the original array.  
So, the score of the new array is \max(A) - \text{second\_min}(A).

Similarly, if we change \max(A), it’s best to change it to \min(A), in which case the score will be \text{second\_max}(A) - \min(A).

The overall answer is the smaller of the two options.

# TIME COMPLEXITY:

\mathcal{O}(N) per testcase.

# CODE:

> **Editorialist's code (PyPy3)**
>
> ```python
> for _ in range(int(input())):
> n = int(input())
> a = list(map(int, input().split()))
> a.sort()
>     
> print(min(a[-2] - a[0], a[-1] - a[1]))
> 
> ```
