LeetCode 1509, Medium. Topics: Array, Greedy, Sorting. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 20 parametrized pytest cases, and a playground notebook:
Problem
<p>You are given an integer array <code>nums</code>.</p>
<p>In one move, you can choose one element of <code>nums</code> and change it to <strong>any value</strong>.</p>
<p>Return <em>the minimum difference between the largest and smallest value of </em><code>nums</code><em> <strong>after performing at most three moves</strong></em>.</p>
Examples
Constraints
- 1 <= nums.length <= 10^5
- -10^9 <= nums[i] <= 10^9
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026