09 Sept 2026Go / Python / TypeScriptEasy

Smallest Range I

Minimize the range after independently changing every value by at most k.

The smallest and largest values can each move inward by k, so clamp their original difference minus two k at zero.

complexity

O(n) time and O(1) space.

solution files

  • Go smallest-range-i/solution.go
  • Python smallest-range-i/solution.py
  • TypeScript smallest-range-i/solution.ts

Solution files

Gosmallest-range-i/solution.go
package main

func smallestRangeI(nums []int, k int) int {
	minimum, maximum := nums[0], nums[0]
	for _, value := range nums[1:] {
		if value < minimum {
			minimum = value
		}
		if value > maximum {
			maximum = value
		}
	}
	result := maximum - minimum - 2*k
	if result < 0 {
		return 0
	}
	return result
}
Pythonsmallest-range-i/solution.py
class Solution:
    def smallestRangeI(self, nums: list[int], k: int) -> int:
        return max(class="syntax-number">0, max(nums) - min(nums) - class="syntax-number">2 * k)
TypeScriptsmallest-range-i/solution.ts
function smallestRangeI(nums: number[], k: number): number {
  return Math.max(class="syntax-number">0, Math.max(...nums) - Math.min(...nums) - class="syntax-number">2 * k);
}