Smallest Range II
MediumGreedygreedy
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
You are given an integer array nums and an integer k. For **every** index i you must either add k to nums[i] or subtract k from it (exactly one of the two).
The *score* of the resulting array is its maximum element minus its minimum element. Return the smallest score you can achieve.
Examples
Input: nums = [1], k = 0
Output: 0
A single element always has score 0.
Input: nums = [0,10], k = 2
Output: 6
Change to [2, 8]; the score is 8 - 2 = 6.
Input: nums = [1,3,6], k = 3
Output: 3
Change to [4, 6, 3]; the score is 6 - 3 = 3.
Constraints
- `1 <= nums.length <= 10^4`
- `0 <= nums[i] <= 10^4`
- `0 <= k <= 10^4`
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.