Tap here to switch tabs
Problem
Submissions

Problem: Smallest Range I

med
30 min
Try to solve the Smallest Range I problem.

Statement

You are given an integer array nums and an integer k.

In one operation, you can choose an integer xx such that −k≤x≤k-k \leq x \leq k for each index i and update nums[i] to nums[i] + x. You can apply this operation at most once for each element.

The score of the array nums is defined as the difference between the maximum element and the minimum element in nums.

Return the minimum possible score of nums after modifying each element.

Constraints:

  • 1≤1 \leq nums.length ≤104\leq 10^4

  • 0≤0 \leq nums[i] ≤104\leq 10^4

  • 0≤0 \leq k ≤104\leq 10^4

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Smallest Range I

med
30 min
Try to solve the Smallest Range I problem.

Statement

You are given an integer array nums and an integer k.

In one operation, you can choose an integer xx such that −k≤x≤k-k \leq x \leq k for each index i and update nums[i] to nums[i] + x. You can apply this operation at most once for each element.

The score of the array nums is defined as the difference between the maximum element and the minimum element in nums.

Return the minimum possible score of nums after modifying each element.

Constraints:

  • 1≤1 \leq nums.length ≤104\leq 10^4

  • 0≤0 \leq nums[i] ≤104\leq 10^4

  • 0≤0 \leq k ≤104\leq 10^4