Minimum Score by Changing Two Elements - Problem

You are given an integer array nums.

The low score of nums is the minimum absolute difference between any two integers.

The high score of nums is the maximum absolute difference between any two integers.

The score of nums is the sum of the high and low scores.

Return the minimum score after changing two elements of nums.

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,4,3]
Output: 0
💡 Note: Change nums to [4,4,4]. Low score = 0 (min difference), High score = 0 (max-min), Total = 0+0 = 0
Example 2 — Different Values
$ Input: nums = [1,4,7,8,5]
Output: 3
💡 Note: Change to [4,4,5,5,5]. Low score = 1 (difference between 4 and 5), High score = 1 (5-4), Total = 1+1 = 2. Actually optimal is changing to get score 3.
Example 3 — Small Array
$ Input: nums = [10,20]
Output: 0
💡 Note: Change both elements to same value, e.g., [20,20]. Low score = 0, High score = 0, Total = 0

Constraints

  • 3 ≤ nums.length ≤ 105
  • 1 ≤ nums[i] ≤ 109

Visualization

Tap to expand
Minimum Score by Changing Two ElementsGoal: Minimize (min_difference + max_range) after changing 2 elementsOriginal Array143Low: min(|1-4|,|1-3|,|4-3|) = 1High: 4-1 = 3, Score: 1+3 = 4Optimal Result333Low: min difference = 0High: 3-3 = 0, Score: 0+0 = 0Change 2 elementsStrategy: Make elements equal to minimize both scoresFinal Answer: 0
Understanding the Visualization
1
Input
Array [1,4,3] with current low and high scores
2
Process
Change two elements to minimize total score
3
Output
Return minimum possible score after changes
Key Takeaway
🎯 Key Insight: Making elements equal or close to equal minimizes both the minimum difference and the range
Asked in
Google 25 Meta 18 Amazon 15
23.4K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen