Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given an integer array nums and an integer k.
In one operation, you can choose any index i where 0 <= i < nums.length and change nums[i] to nums[i] + x where x is an integer from the range [-k, k]. You can apply this operation at most once for each index i.
The score of nums is the difference between the maximum and minimum elements in nums.
Return the minimum score of nums after applying the mentioned operation at most once for each index in it.
Example 1:
Input: nums = [1], k = 0 Output: 0 Explanation: The score is max(nums) - min(nums) = 1 - 1 = 0.
Example 2:
Input: nums = [0,10], k = 2 Output: 6 Explanation: Change nums to be [2, 8]. The score is max(nums) - min(nums) = 8 - 2 = 6.
Example 3:
Input: nums = [1,3,6], k = 3 Output: 0 Explanation: Change nums to be [4, 4, 4]. The score is max(nums) - min(nums) = 4 - 4 = 0.
Constraints:
1 <= nums.length <= 1040 <= nums[i] <= 1040 <= k <= 104Problem summary: You are given an integer array nums and an integer k. In one operation, you can choose any index i where 0 <= i < nums.length and change nums[i] to nums[i] + x where x is an integer from the range [-k, k]. You can apply this operation at most once for each index i. The score of nums is the difference between the maximum and minimum elements in nums. Return the minimum score of nums after applying the mentioned operation at most once for each index in it.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[1] 0
[0,10] 2
[1,3,6] 3
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #908: Smallest Range I
class Solution {
public int smallestRangeI(int[] nums, int k) {
int mx = 0;
int mi = 10000;
for (int v : nums) {
mx = Math.max(mx, v);
mi = Math.min(mi, v);
}
return Math.max(0, mx - mi - k * 2);
}
}
// Accepted solution for LeetCode #908: Smallest Range I
func smallestRangeI(nums []int, k int) int {
mi, mx := slices.Min(nums), slices.Max(nums)
return max(0, mx-mi-k*2)
}
# Accepted solution for LeetCode #908: Smallest Range I
class Solution:
def smallestRangeI(self, nums: List[int], k: int) -> int:
mx, mi = max(nums), min(nums)
return max(0, mx - mi - k * 2)
// Accepted solution for LeetCode #908: Smallest Range I
impl Solution {
pub fn smallest_range_i(nums: Vec<i32>, k: i32) -> i32 {
let max = nums.iter().max().unwrap();
let min = nums.iter().min().unwrap();
(0).max(max - min - k * 2)
}
}
// Accepted solution for LeetCode #908: Smallest Range I
function smallestRangeI(nums: number[], k: number): number {
const mx = Math.max(...nums);
const mi = Math.min(...nums);
return Math.max(mx - mi - k * 2, 0);
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.